Microtask Lab
See why promise callbacks run before setTimeout(…, 0).
Phase 10 taught the task queue (macrotasks) for timers.
Promise handlers use a separate microtask queue.
After the current stack empties, the engine drains microtasks
before the next timer task. So:
sync → promise .then → setTimeout.
Call stack
Microtasks
Task queue
Press Run — expect: A, C, then promise B, then timeout D.
Important JavaScript
console.log("A");
Promise.resolve().then(function () {
console.log("B"); // microtask
});
console.log("C");
setTimeout(function () {
console.log("D"); // macrotask
}, 0);
// Prints: A, C, B, D