22

Would it make any difference if I have:

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

instead of:

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 either. E.g. I can do this for either case:

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

but I am more curious about the underlying mechanism how they both behave the same.

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

3 Answers 3

10

I think you're effectively calling synchronous-like functions with await within the promise chain which, according to this answer:

You are perfectly free to call either synchronous functions within the promise chain (from within .then() handlers) or asynchronous functions that then return a new promise.

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

Sign up to request clarification or add additional context in comments.

Comments

8

I would think the way async functions works in respect to the return value is it checks whether that value is wrapped in a Promise object, and if not, does it automatically. If you explicitly return a Promise, then the function does nothing with it. I tested this out by returning a new Promise inside an async function:

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

var neverresolved = test()

The result is neverresolved contains a Promise that's always in the pending state, as expected.

Comments

1

Both functions return a Promise.

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

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

await is an alternative to call then on Promise and its form is also more readable than

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

});

If you have multiple sequentially requests using await is a better choice

var response1= await promise1   
var response2=await promise2

against

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

EDIT

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

The second function will return a Promise where you see the keyword await. When the awaited promise will resolved the execution inside the function continue and you can access to result of the promise

EDIT 1

MaybeI understand what you mean, the async keyword encapsulate your return value into a promise that has as resolved value what you have returned

8 Comments

Yep, I understand that. However, if saying async always returns a promise, it means it will wrap the returned promise inside another promise?
Promise.all create a new promise that is the same that will return, await will simply pause the execution of the function until the value from the promise is available.
I think you misunderstood my question. I know about Promise.all, and how to use async functions. My question is more like: with the first case, I return a value which the async function would resolve to. As for the second case, what exactly happens?
Not really what I'm asking. It looks useless because I gave a simple example, what if in between, I need to do an await? then the async won't be useless.
The async became useless if you not use await inside
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.