When does async function actually return a pending promise?

An asynchronous function runs synchronously till it reaches the first await (or the end of the function). Then it returns a new unresolved Promise, that will be resolved somewhen later when the function finished execution. The execution of the asynchronous function then halts, and synchronous execution of the code that called the async function continues.

this line inner(await main()) will immediately return a pending promise but the code inside main() will continue to execute until it gets to the first await keyword, is that correct?

No. First main() will be executed synchronously till the code inside the async main function reaches an await, then it’ll return a promise and execution of f continues. As then the await gets reached f also itself returns a new Promise and stops execution.

Semantic details can be found in the specification.

Leave a Comment