Understanding process.nextTick()

The Node.js process.nextTick function interacts with the event loop in a special way

When you try to understandNode.js event loop, One of the important parts isprocess.nextTick().

Whenever the event loop goes through a full pass, we call it a "tick".

When we pass a function toprocess.nextTick(), We instruct the engine to call this function at the end of the current operation before the next event loop tick starts:

process.nextTick(() => {
  //do something

The event loop is busy processing the current function code.

After the operation is over, the JS engine will pass the operation tonextTickCalled during this operation.

This is how we can tell the JS engine to process a function asynchronously (after the current function), but please enqueue it as soon as possible.

callsetTimeout(() => {}, 0)The function will be executed at the end of the next tick mark, much later than when usednextTick()It will prioritize the call and execute it before the next tick starts.

usenextTick()When you want to make sure that the code has been executed in the next iteration of the event loop.

Download mine for freeNode.js manual

More node tutorials: