πŸ”΄ HardEvent Loop & PromisesπŸ’» Output Question

async/await with rejection

πŸ’‘

Hint

Throwing inside an async function rejects its returned Promise. try/catch around await catches rejected Promises just like sync exceptions.

What does this output?

async function failing() {
  throw new Error('oops');
}

async function main() {
  console.log('A');
  try {
    await failing();
    console.log('B');
  } catch(e) {
    console.log('C:', e.message);
  }
  console.log('D');
}

main();
console.log('E');

Correct Output

A
E
C: oops
D

Why this output?

Explanation: "A" β€” sync. await failing() suspends main. "E" β€” sync resumes. Microtask: failing() rejected β€” caught β†’ "C: oops". After catch, continues: "D". "B" is skipped β€” throw jumps to catch.

Key Insight: Throwing inside an async function rejects its returned Promise. try/catch around await catches rejected Promises just like sync exceptions.

More Event Loop & Promises Output Questions

🟑 MediumClassic setTimeout vs Promiseβ†’πŸ”΄ HardNested setTimeout and Promiseβ†’πŸŸ‘ Mediumasync/await execution orderβ†’πŸ”΄ HardPromise chaining orderβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz