process.nextTick vs setImmediate in Node.js
Compare process.nextTick and setImmediate in Node.js: execution order, the event loop check phase, starvation risks, and when to use each.
Expected Interview Answer
process.nextTick callbacks run immediately after the current operation completes and before the event loop continues, while setImmediate callbacks run on the next iteration of the event loop during the check phase — so nextTick always fires first.
process.nextTick does not belong to a phase of the event loop; its queue is drained completely after each operation and before the loop moves on, even before resolved Promise microtasks in older behavior. setImmediate is a true event-loop callback scheduled in the check phase, which runs after the poll phase. Because the nextTick queue is fully processed between phases, recursively scheduling nextTick can starve the event loop and block I/O, whereas setImmediate yields to the loop between calls.
- nextTick lets you defer work until right after the current stack unwinds
- setImmediate yields to I/O so it won't starve the loop
- Clear ordering guarantees for cleanup and error handling
- nextTick is useful for making APIs consistently asynchronous
- setImmediate is ideal for breaking up long-running CPU work
AI Mentor Explanation
process.nextTick is the umpire's instant signal after the very ball just bowled, settled before anything else. setImmediate is a decision reviewed at the end of the over, once the current over's deliveries are all done. The instant call always resolves before play moves on to the next over.
Step-by-Step Explanation
Step 1
Current operation finishes
The currently executing JavaScript stack runs to completion first.
Step 2
Drain the nextTick queue
All process.nextTick callbacks run, and any they schedule, before the loop continues.
Step 3
Process microtasks
Resolved Promise callbacks run next, after nextTick and before the loop proceeds.
Step 4
Event loop phases run
Timers, pending callbacks, poll and other phases execute in order.
Step 5
Check phase runs setImmediate
In the check phase, setImmediate callbacks fire — on the next loop iteration after the poll phase.
What Interviewer Expects
- That nextTick runs before setImmediate
- nextTick is not part of an event-loop phase; setImmediate is the check phase
- Awareness that recursive nextTick can starve I/O
- Correct ordering relative to Promise microtasks
- When to choose each in real code
Common Mistakes
- Claiming setImmediate runs before nextTick
- Thinking nextTick is part of the event loop's phases
- Ignoring that recursive nextTick can block I/O entirely
- Confusing setImmediate with setTimeout(fn, 0)
- Assuming the ordering is non-deterministic
Best Answer (HR Friendly)
“Both schedule code to run later, but process.nextTick runs almost right away, before Node moves on to other work, while setImmediate waits for the next cycle of Node's event loop. Because of that, nextTick callbacks always run before setImmediate ones.”
Code Example
console.log('start');
setImmediate(() => {
console.log('setImmediate');
});
process.nextTick(() => {
console.log('nextTick');
});
Promise.resolve().then(() => {
console.log('promise');
});
console.log('end');
// Output:
// start
// end
// nextTick <- runs right after the current stack
// promise <- microtask, after nextTick
// setImmediate <- next event loop iteration (check phase)Follow-up Questions
- What are the phases of the Node.js event loop?
- How can recursive process.nextTick starve the event loop?
- How does setImmediate differ from setTimeout(fn, 0)?
- Where do Promise microtasks run relative to nextTick?
- When would you deliberately choose setImmediate over nextTick?
MCQ Practice
1. Which runs first when both are scheduled from the same synchronous code?
The nextTick queue is drained after the current operation and before the event loop continues, so it runs before setImmediate.
2. In which event loop phase does setImmediate execute?
setImmediate callbacks are invoked in the check phase, which comes right after the poll phase.
3. Why can recursive process.nextTick be dangerous?
Because the nextTick queue is fully drained before the loop proceeds, endlessly re-scheduling it prevents I/O and timers from ever running.
Flash Cards
Which fires first, nextTick or setImmediate? — process.nextTick — its queue drains before the event loop continues.
Is process.nextTick part of an event loop phase? — No. It runs between operations, outside the phases; setImmediate runs in the check phase.
What risk does recursive nextTick pose? — It can starve the event loop, blocking I/O and timers from executing.
Where does setImmediate run? — In the check phase of the event loop, after the poll phase, on the next iteration.