Javascript: return a promise inside an asynchronous function

Will it matter if I have:

async function test () { const foo = await bar() return Promise.all([promise1, promise2]) } 

instead:

 async function test () { const foo = await bar() const [result1, result2] = await Promise.all([promise1, promise2]) // Given that I don't care about result1, result2 in this `test` function return [result1, result2] } 

I get the same result if I do this. For instance. I can do this for both cases:

 test().then(([result1, result2]) => { ... }) 

but I'm more interested in the underlying mechanism of how they behave the same.

In other words, how does the async function handle it if inside the function I return a promise instead of a value?

+6
source share
3 answers

I think you are calling a synchronous function (one with await turns it into a function that returns a value) in a promise chain.

from this answer :

You can freely call either synchronous functions inside (from .then () handlers) or asynchronous functions, which then returns a new promise.

When you return something from the .then () handler, you can either return the value (which becomes the resolved value of the parent promise), or you can return another promise (which chains to the previous promise), or you can throw what works like return rejected promise (promise chain rejected).

The second case is the one in which you call the synchronous method from test().then(...) , which is valid.

0
source

Both functions return Promise .

 const [result1, result2] = await Promise.all([promise1, promise2]) //HERE return [result1, result2] 

Where I wrote HERE, you can access result1 and result2 var, which are the results of promises.

Waiting is an alternative to calling then on Promise , and its form is also more readable than

 Promise.all([promise1, promise2]).then(function(results){ }); 

If you have multiple consecutive requests using await, this is the best choice.

 var response1= await promise1 var response2=await promise2 

vs

 promise1.then(function(){ promise2.then(function(){ promise3.then(function(){ }) }) }) 

EDIT

In the first function, the async keyword is useless, the test function will return Promise

The second function will return a promise in which you will see the await keyword. When the expected promise is resolved, execution within the function continues, and you can access the result of the promise

EDIT 1

Perhaps I understand what you mean, the async keyword encapsulates the return value in a promise that has as the permitted value that you returned.

+2
source

I would think that async functions work with respect to the return value, checks to see if that value is completed in the Promise object, and if not, does it automatically. If you explicitly return Promise, then the function does nothing with it. I checked this by returning a new Promise inside an asynchronous function:

 async function test(){ var duration = resolveAfter(500) return new Promise((resolve, reject) =>{}) } var neverresolved = test() 

The result is never resolved, contains a promise that is always in a pending state, as expected.

+1
source

Source: https://habr.com/ru/post/1014064/


All Articles