nextTick(callback) is a method provided by the Async module that defers the execution of a function until the next event loop iteration. Similar to how the setTimeout() is used in browsers to defer execution of a function by a given time, nextTick() defers execution until the next tick.
In Node.js, each iteration of an event loop is called a tick.
In Node.js, nextTick() just calls process.nextTick. In the browser, it will use setImmediate if available, otherwise it will use setTimeout(callback, 0), which means that the other higher priority events may precede the execution of the callback function.
The function signature is as follows:
nextTick(callback, arg...)
callback: The function to call on a later loop around the event loopargs...: (Optional) Additional arguments to pass to the callbackIn this example, we simply defer the execution of a function that accepts a name argument and output that name to the console.
Notice that the statement console.log('1. Hello') runs before the statement console.log('2.', name) inside the callback function passed to nextTick().
import nextTick from 'async/nextTick';// Defering the execution of a function.nextTick(function(name) {// This will run secondconsole.log('2.', name);}, 'World');// This will run firstconsole.log('1. Hello');
Free Resources