Skip to content

OutputNot attempted

Sync vs microtask vs macrotask

console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve()
.then(() => {
console.log(3);
setTimeout(() => console.log(4), 0);
})
.then(() => console.log(5));
queueMicrotask(() => console.log(6));
(async () => {
console.log(7);
await null;
console.log(8);
})();
console.log(9);

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

All sync logs first (1 7 9). Microtasks run in queue order: 3, 6, 8; 5 is queued only when 3's callback returns. Then timers: 2, then 4 (queued during a microtask, so after 2).