Beginner Node.js Interview QuestionsBeginnerComparison
Node.js · Question 6
What is the difference between process.nextTick and setImmediate?
Direct answer
process.nextTick runs before the next event loop phase continues — highest priority, can starve I/O; setImmediate runs in the check phase after poll, designed to defer work until after current I/O callbacks finish.
Both schedule callbacks asynchronously, but they sit in different queues with different priority. nextTick is not part of libuv phases — Node drains the entire nextTick queue between every phase transition.
| process.nextTick | setImmediate | |
|---|---|---|
| Queue | nextTick queue (Node-specific) | check phase of event loop |
| Priority | Higher — before Promises and next phase | After poll phase |
| Use for | Defer error propagation, run after sync assign | Split long sync work across turns |
| Risk | Recursive nextTick starves I/O | Safer for yielding; still use workers for CPU |
nexttick-vs-immediate.js
console.log("start");
setImmediate(() => console.log("setImmediate"));
process.nextTick(() => console.log("nextTick 1"));
process.nextTick(() => console.log("nextTick 2"));
Promise.resolve().then(() => console.log("promise"));
console.log("end");Output
start end nextTick 1 nextTick 2 promise setImmediate
- nextTick use case — emit 'error' on next tick so all 'error' listeners attach first (EventEmitter pattern).
- setImmediate use case — break up sync CPU without blocking poll as aggressively as nextTick chains.
- queueMicrotask — standard microtask API; runs after nextTick, before macrotasks.