GAP·MAP
← all breakdowns
Node.jsEvent LoopAsync Schedulingmiddle level

nextTick vs setImmediate

the question

What is the difference between `process.nextTick()` and `setImmediate()` in Node.js, and when would you use each?

what they're testing: The interviewer is testing whether you can reason about callback scheduling, event-loop fairness, and the practical cost of choosing the wrong queue.

a strong answer

process.nextTick() runs sooner: it queues a callback to run after the current JavaScript operation finishes, before Node lets the event loop continue. setImmediate() queues a callback for the check phase, after the poll phase has had a chance to process I/O. Despite the names, nextTick is more immediate.

That ordering drives the choice. Use nextTick only when a callback must wait until the stack unwinds but still run before I/O, such as preserving an always-asynchronous API or letting callers attach event listeners. Its queue drains completely, so recursively scheduling nextTick can keep Node from reaching poll and starve I/O. setImmediate yields to the loop first, making it easier to reason about and better for breaking work across iterations. An immediate scheduled inside an immediate waits until the following iteration.

For new userland deferral, Node marks nextTick legacy and generally recommends queueMicrotask() instead. That does not make queueMicrotask and nextTick identical: they use different queues, and their ordering differs between CommonJS and ES modules. If the real need is “run after I/O gets a chance,” setImmediate is still the clear fit.

Where people slip

the tempting wrong answer, and what's actually true

  • `process.nextTick()` runs on the next iteration of the event loop.

    Its queue drains after the current operation completes and before the event loop is allowed to continue.

  • `setImmediate()` runs before `process.nextTick()` because it is immediate.

    A `nextTick` callback runs before the loop continues, while an immediate waits for the check phase.

  • Recursive `process.nextTick()` calls are harmless because each callback is asynchronous.

    Recursively refilling the next-tick queue can prevent the loop from reaching poll and starve I/O.

  • The two APIs are interchangeable, so the choice is only stylistic.

    They schedule work at different points: `process.nextTick()` runs before the event loop continues, while `setImmediate()` runs in the check phase.

If they push further

How does `setImmediate()` differ from `setTimeout(fn, 0)`?

`setImmediate()` runs in the check phase, while the timeout runs in the timers phase after its minimum threshold; their top-level order is not guaranteed, but an immediate scheduled in an I/O callback runs first.

Where do Promise callbacks and `queueMicrotask()` fit?

They use the microtask queue rather than the next-tick queue, and Node drains microtasks after next ticks; module context can change the observed order because ES modules already execute as microtasks.

How can `process.nextTick()` starve I/O?

Node fully drains the next-tick queue before continuing the event loop, so a callback that keeps adding another next tick can indefinitely delay the poll phase.

Sources

Now answer it yourself.

Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.

was this useful?

beta

The interviewer part is in the works.

The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.