What is process.nextTick in NodeJS?

The process.nextTick serves for the design logic where for one, events that are generated by while initialising or creating an object is not generated before the caller has registered listeners for those.

Now considering the above case, there is a possible that the disk reader is just for a specific case reading from cache and the inner logics of callbacks happen immediately as soon as the object is created via diskReader() call.

But the actual object does not have the proper handler registered for this event yet. So though the “readcomplete” is fired, its gone amiss and still the dataReader might still be in a state assuming read is not complete yet.

So if we use this as

this means the execution flow of current script is done only then (on the next tick) these code are executed. So emitter.emit(‘readcomplete’) is just put in a queue and current script is complete executed till dataReader.on(“readcomplete”) line is executed. Then the queue of the nextTick calls are executed in sequence. The queue of nextTick Calls is limited by process.maxTickDepth.

Leave a Reply