Skip to content

QuizNot attempted

nextTick vs promises vs timers

What does this print in Node.js?
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
Promise.resolve().then(() => console.log('promise'));
process.nextTick(() => console.log('nextTick'));
console.log('sync');
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • sync, nextTick, promise, then timeout and immediate in either order
  • Order is: sync code, then the process.nextTick queue (Node drains it before promises), then promise microtasks, then the loop phases.
  • In the main module setTimeout(0) vs setImmediate is non-deterministic: it depends on whether the 1ms timer is already due when the loop enters the timers phase.
  • Not promise before nextTick: nextTick has priority over the promise queue.
  • Not nextTick before sync: nothing async runs until the current script finishes.
  • Not timers before nextTick/promise: both microtask-style queues drain before any loop phase.