codecks.

Async & promises · Output question

JavaScript async functions before and after await

An async function logs on both sides of await. Predict how its continuation fits around the caller’s synchronous work.

async function announce() {
  console.log("inside");
  await 0;
  console.log("resumed");
}
announce();
console.log("outside");

What gets logged?

Choose an output before opening the walkthrough, or check your answer in practice mode.

  1. inside
    resumed
    outside
  2. outside
    inside
    resumed
  3. resumed
    inside
    outside
  4. inside
    outside
    resumed
Reveal the answer and execution trace
inside
outside
resumed

An async function runs synchronously until await. Even awaiting a plain value resumes the function asynchronously.

  1. Line 6. Calling announce enters the function immediately.
  2. Line 2. The function logs "inside".
  3. Line 3. await suspends this function and schedules its continuation.
  4. Line 7. The outer script logs "outside" before the function resumes and logs "resumed".

Reference: Await control flow on MDN.