setTimeout vs setImmediate vs process.nextTick in Node.js
These three scheduling functions run callbacks at different times. Here is the difference and order.
setTimeout vs setImmediate vs process.nextTick in Node.js
setTimeout, setImmediate, and process.nextTick schedule callbacks at different times in the event loop. Here is the difference and execution order.
setTimeout
Schedules a callback in the timers phase, after a delay. setTimeout(fn, 0) runs on the next tick's timers phase, after the current stack and all microtasks complete.
setImmediate
Schedules a callback in the check phase, right after the poll phase. setImmediate runs after I/O events are processed in the current tick, before the next timers phase.
process.nextTick
Schedules a callback to run after the current operation, before the next phase of the event loop and before promise microtasks. It is the most urgent of the three.
Execution Order
In general: process.nextTick runs first (before promises and before the next event loop phase), then promises, then the event loop phases (timers, poll, check). So nextTick, then promises, then setImmediate, then setTimeout on the next tick.
When to Use Each
Use setImmediate to run code after the current I/O events. Use setTimeout for a delayed callback. Use process.nextTick rarely, since it can starve the event loop by running before I/O. Most code uses setImmediate or async/await.
The Common Confusion
People confuse setImmediate and nextTick. setImmediate runs in the check phase after I/O. nextTick runs before the next phase, even before I/O. nextTick is more urgent but can starve the loop if overused.
The Takeaway
setTimeout runs in timers phase after a delay. setImmediate runs in check phase after I/O. process.nextTick runs before the next phase, even before promises. Order: nextTick, promises, then event loop phases. Use setImmediate for most deferred work.
setTimeout runs in the timers phase after a delay. setImmediate runs in the check phase after I/O events. process.nextTick runs before the next event loop phase, even before promise microtasks. Order: nextTick, promises, then event loop phases.
On the next event loop tick, setImmediate usually runs before setTimeout(0) when there are no pending timers, because setImmediate runs in the check phase right after poll, while setTimeout runs in the timers phase at the start of the next tick.
Rarely. It runs before the next event loop phase, even before I/O, which can starve the loop if overused. Most code should use setImmediate or async/await instead. nextTick is mainly for internal library APIs that need to run before any I/O.
Because it runs before the next event loop phase, even before I/O. If you recursively call nextTick, the event loop never reaches the poll phase to process I/O, starving all connections. This is why it should be used sparingly.
To run code after the current I/O events in the same tick. It is the standard way to defer work to the next check phase, letting the poll phase process I/O first. Use it when you need to run code after current I/O but before the next timer.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

