What is process.nextTick() in Node.js?
Answer
process.nextTick(callback) adds the callback to the nextTick queue, which is processed immediately after the current operation completes — before the event loop moves to the next phase, before any I/O, timers, or setImmediate callbacks. It is the highest-priority async scheduling mechanism. Use cases: (1) Allow a function to complete before a caller's callback runs; (2) Allow an EventEmitter's "error" event to be caught after the emitter is set up (avoid emitting events before listeners are attached); (3) Allow cleanup code to run before more I/O is processed. Warning: overusing process.nextTick() can starve the event loop — if nextTick callbacks keep adding more nextTick callbacks, I/O events are never processed (use setImmediate() to yield to I/O). The distinction: nextTick fires before the next event loop iteration; setImmediate fires in the next iteration's check phase.
Previous
What is the difference between setTimeout and setImmediate in Node.js?
Next
What is the cluster module in Node.js?