🟑 MediumEvent Loop & PromisesπŸ’» Output Question

async/await execution order

πŸ’‘

Hint

Everything before the first await in an async function runs synchronously.

What does this output?

async function foo() {
  console.log('foo start');
  await Promise.resolve();
  console.log('foo end');
}

console.log('1');
foo();
console.log('2');

Correct Output

1
foo start
2
foo end

Why this output?

Explanation: foo() runs synchronously until the first await. The await suspends foo and returns control. "2" prints. Then the microtask resumes foo and prints "foo end".

Key Insight: Everything before the first await in an async function runs synchronously.

More Event Loop & Promises Output Questions

🟑 MediumClassic setTimeout vs Promiseβ†’πŸ”΄ HardNested setTimeout and Promiseβ†’πŸ”΄ HardPromise chaining orderβ†’πŸŸ‘ MediumPromise constructor is synchronousβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz