setImmediate(callback)
is a method provided by the Async module that defers a callback function’s execution until the next event loop iteration.
In Node.js, this is called setImmediate
. The browser will use setImmediate
if available; otherwise, setTimeout(callback, 0)
will be used (this means other higher priority events may precede the execution of the callback
function).
setImmediate(callback, args...)
callback
: The function to call on a later loop around the event loop.args...
: (Optional) Additional arguments to pass to the callback.In this example, we defer the execution of a function that accepts a name
argument and outputs 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 setImmediate()
.
import setImmediate from 'async/setImmediate';// Defering the execution of a function.setImmediate(function(name) {// This will run secondconsole.log('2.', name);}, 'World');// This will run firstconsole.log('1. Hello');
For information on
nextTick(callback)
, a function similar tosetImmediate(callback)
in the Async module, click here.
Free Resources