Using nextTick to Schedule Work
A very useful method of scheduling work on the event queue is using the process.nextTick(callback) function. This function schedules work to be run on the next cycle of the event loop. Unlike the setImmediate() method, nextTick() executes before the I/O events are fired. This can result in starvation of the I/O events, so Node.js limits the number of nextTick() events that can be executed each cycle through the event queue by the value of process.maxTickDepth, which defaults to 1000.
The code in Listing 4.3 illustrates the order of events when using a blocking I/O call, timers, and nextTick(). Notice that the blocking call fs.stat() is executed first, then two setImmediate() calls, and then two nextTick() calls. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access