1

If I have an async-function which contains an explicit return statement which returns a Promise, what will be the value of an await expression calling such a function?

Does the await resolve to the value given by the explicit return-expression of the async-function, or does it resolve to what that value resolves to in case it is a Promise?

I know there are already questions related to this topic on Stack Overflow, but I would just like a precise answer to this question: "Is it possible for an 'await' to resolve to a Promise?". Yes or No? Of course if you can also explain why or why not, that would be very helpful.

Panu Logic
  • 2,193
  • 1
  • 17
  • 21
  • The whole point with async/await is to give syctactic sugar to Promises – ShamPooSham May 29 '21 at 20:47
  • "*an explicit return statement which returns a Promise*" - you might want to have a look at [this](https://stackoverflow.com/q/43353087/1048572) or [that](https://stackoverflow.com/q/38708550/1048572) question about what happens there – Bergi May 29 '21 at 20:53

3 Answers3

2

No, it's impossible for an await expression to result in a promise.

Just like you cannot fulfill a promise with another promise, and like then() never calling the fulfillment handler with a promise.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • So if the explicit return statement of an async -function returns a Promise, then the value of the await -expression in the caller of that function would resolve into what the explicitly returned promise would resolve to, not to that promise. Right? – Panu Logic May 29 '21 at 21:48
  • 1
    Yes. The `await` expression never even sees the inner promise, it only sees the promise that is [immediately created by the call to the `async function`](https://stackoverflow.com/q/35302431/1048572). When (later) the `return` statement is executed, it just [resolves](https://stackoverflow.com/q/29268569/1048572) that first ("outer") promise with the inner promise, which causes the first promise to [fulfill/reject](https://stackoverflow.com/q/29268569/1048572) with the same value later. Only when that happens, the `await` is notified to resume the caller. – Bergi May 29 '21 at 21:55
1

If you use async syntax, that means that the function will return a Promise. The async/await is just a different syntax for handling Promises, which allow you to write synchronous-like code.

So, it is the same if you return a value from async function, or you return a resolved/rejected Promise from non-async function.

NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • That is very helpful. I read somewhere too that the async-function will (always) "return a Promise". My confusion was as to which promise is returned, who creates that promise, and when and when not. It now seems clear to me that if the async-function explicitly returns a Promise, then no NEW promise will be created around that. – Panu Logic May 29 '21 at 21:58
-1

If you define the function with async, it wraps a Promise around whatever the function body returns.

If the function does return new Promise(...) then that Promise will be wrapped in another Promise added by async.

await resolves a Promise, but if the Promise contains another Promise, the value of the await expression will be the nested Promise.

Barmar
  • 741,623
  • 53
  • 500
  • 612