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?

BPm
  • 2,924
  • 11
  • 33
  • 51

3 Answers3

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).

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
7

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.

coco puffs
  • 1,040
  • 12
  • 8
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

Marco Moretti
  • 689
  • 4
  • 11
  • 3
    Yep, I understand that. However, if saying async always returns a promise, it means it will wrap the returned promise inside another promise? – BPm Jan 13 '17 at 16:27
  • 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. – Marco Moretti Jan 13 '17 at 16:32
  • 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? – BPm Jan 13 '17 at 16:33
  • 2
    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. – BPm Jan 13 '17 at 16:54
  • The async became useless if you not use await inside – Marco Moretti Jan 13 '17 at 16:54
  • The example can easily be changed to something that has a few awaits inside and not really the point of my question. How does async function handle it when inside, I return a promise instead a value? – BPm Jan 13 '17 at 16:57
  • If you simply return a promise,the async do nothing. Async only works in union with await in the same way await can works only with async – Marco Moretti Jan 13 '17 at 17:04
  • Please see the updated examples, there are other awaits inside the function. – BPm Jan 13 '17 at 17:06