AceDevHub
Advanced Node.js Interview QuestionsAdvancedCode Output

Node.js · Question 85

What is the output order of this Node.js event loop code?

Direct answer

Output order is 1, 5, 4, 3, 2 — synchronous code first, then process.nextTick queue, then Promise microtasks, then setTimeout timers phase.

This classic snippet tests event loop phase priority — ties directly to Q4–6 on the loop, nextTick, and timers.

event-loop-order.mjs
console.log("1");

setTimeout(() => console.log("2"), 0);

Promise.resolve().then(() => console.log("3"));

process.nextTick(() => console.log("4"));

console.log("5");
Output
1
5
4
3
2
  1. Sync — 1 and 5 run immediately in current tick.
  2. nextTick queue — 4 drains before other microtasks (Node-specific, highest priority).
  3. Microtasks — Promise.then → 3.
  4. Timers phase — setTimeout callback → 2.