Async & promises · Output question
JavaScript promises, timers, and synchronous output
A promise reaction and a zero-delay timer are scheduled beside synchronous logs. Predict which queued callback runs first.
console.log("open");
setTimeout(() => console.log("timer"), 0);
Promise.resolve().then(() => console.log("promise"));
console.log("close");What gets logged?
Choose an output before opening the walkthrough, or check your answer in practice mode.
Reveal the answer and execution trace
open
close
promise
timerThe current script finishes, then the promise reaction runs as a microtask, then the timer callback runs as a task.
- Line 1. The script logs "open" immediately.
- Line 2. The timer callback is scheduled for a later task.
- Line 3. The fulfilled promise schedules a reaction microtask.
- Line 4. The script logs "close". After the script ends, the promise runs before the timer.
Reference: Tasks and microtasks on MDN.