Nested setTimeout creates new macrotask
HardEvent Loop & Promises
Code
setTimeout(() => {
console.log('outer');
setTimeout(() => console.log('inner'), 0);
}, 0);
console.log('sync');Your prediction (one output per line)
A setTimeout inside a setTimeout callback schedules a new macrotask — runs in a future event loop iteration.
Explanation: sync runs first. outer fires in macrotask 1. Inside it, inner is registered as a new macrotask. inner runs in macrotask 2.
Key Insight: A setTimeout inside a setTimeout callback schedules a new macrotask — runs in a future event loop iteration.
66 output questions with instant feedback