← Back to Node.js, page 1

nodejs-0004 · Hard senior

Why does the order of setTimeout(fn, 0) and setImmediate(fn) change between runs, and when is it guaranteed?

At the top level of a script, setTimeout 0 is really a 1 ms timer. Whether that millisecond has passed when the loop first enters the timers phase depends on process speed, so either callback can run first. Inside an I/O callback, or inside a timer callback, setImmediate always runs first.

Full answer

Node turns a delay of 0 into 1 ms. When the main script ends, the event loop starts at the timers phase. If at least 1 ms has passed since setTimeout was called, the timer fires first. If the machine was fast, the timers phase finds nothing ready, the loop moves on, and the check phase runs setImmediate first. Startup cost, system load and CPU speed change the result from run to run.

The order is fixed in two cases:

  • Inside an I/O callback (poll phase). The next phase is check, so setImmediate runs before the timer, which must wait for the next iteration.
  • Inside a timer callback. The loop is in the timers phase. A new setTimeout 0 is due at least 1 ms later, so it waits for the next iteration, while check comes next in the current one, so setImmediate runs first.

There is a second, newer rule worth knowing. Since Node 11, the nextTick and promise queues are emptied after each individual callback, not only after a whole phase. Before Node 11, two setImmediate callbacks would both run before any promise callback. Today a promise callback created in the first immediate runs before the second immediate.

If you need an order, do not depend on timers. Chain the work with promises or callbacks so the order is explicit.

javascript
const fs = require('node:fs');

// Case 1: microtasks run between two callbacks of the same phase (Node 11 and later).
function caseOne(next) {
  setImmediate(() => {
    console.log('A1. first immediate');
    Promise.resolve().then(() => console.log('A2. promise from first immediate'));
  });
  setImmediate(() => {
    console.log('A3. second immediate');
    next();
  });
}

// Case 2: inside a timer callback, setImmediate beats a new setTimeout 0.
function caseTwo() {
  setTimeout(() => {
    setTimeout(() => console.log('B3. inner setTimeout 0'), 0);
    setImmediate(() => console.log('B2. inner setImmediate'));
    console.log('B1. outer timer callback');
  }, 10);
}

caseOne(caseTwo);

Both cases are fixed in order on current Node versions. The top-level setTimeout vs setImmediate race is not printed here because it is not stable. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): A1. first immediate A2. promise from first immediate A3. second immediate B1. outer timer callback B2. inner setImmediate B3. inner setTimeout 0

Key points

  • setTimeout 0 is treated as 1 ms, so the first-iteration result depends on timing.
  • Inside an I/O callback, setImmediate always runs first.
  • Inside a timer callback, setImmediate also runs first in practice, because check follows timers.
  • Microtasks run after every callback since Node 11, not after every phase.

Common mistakes

  • Saying the order is 'random'. It is timing dependent, and fixed in the cases above.
  • Writing tests that depend on the top-level order.
  • Forgetting that a zero delay is clamped to 1 ms.
  • Using old advice that all immediates run before any promise callback.

Follow-ups

  • How would you rewrite code that depends on the timer vs immediate order?
  • What changed in Node 11 about microtasks and timers?
  • Why can a very busy process make setTimeout 0 fire later than setImmediate?

Tips

  • Explain the 1 ms clamp, then the two guaranteed cases.
  • Show you can reason about it from the phase order instead of memorising outputs.
  • Offer the fix: make the dependency explicit with promises.