What is the difference between process.nextTick, setImmediate and setTimeout with zero delay?
They are three different queues with three different priorities, and the distinction comes up constantly in Node interviews.
process.nextTick— not part of the event loop at all. Its queue is drained after the current operation completes and before the loop continues to the next phase. It has the highest priority, and it runs before promise microtasks.setImmediate— runs in the check phase, which is immediately after the poll phase. If you want to yield to I/O and then run, this is the correct choice.setTimeout(fn, 0)— runs in the timers phase. The zero is treated as one millisecond, so it is not really immediate.
The subtlety interviewers look for: at the top level of a script, the order of setTimeout(fn, 0) and setImmediate(fn) is non-deterministic, because it depends on how long the process took to start. Inside an I/O callback, however, setImmediate always fires first, because the loop is already past the timers phase and heading into check.
Note: Recursive process.nextTick calls will starve the event loop entirely. setImmediate will not.





