CodePark/ questions / Event loop phases
START PRACTICINGPRACTICE
Backend question · Node.js › Event loop phases

What does this log?

console.log("start");
setImmediate(() => console.log("immediate"));
Promise.resolve().then(() => console.log("promise"));
process.nextTick(() => console.log("tick"));
console.log("end");
start end tick promise immediate

"start end tick promise immediate" is correct. Synchronous code runs first, so start and end come first. When the main script finishes, Node empties the process.nextTick queue before the promise microtask queue. So tick comes before promise, even though the promise was scheduled first. This is why "start end promise tick immediate" is wrong. setImmediate waits for the check phase of the event loop, so immediate is last.