There are four kinds of deferral mechanisms in Node.js:
setTimeout
setInterval
setImmediate
process.nextTick
setTimeout
and setInterval
are quite familiar to those used to JavaScript in the browser and their semantics are fairly well understood. They return opaque values that can be passed to their clear
counterparts, and have been around forever. setImmediate
is a newer construct and its adoption in the browser is not very wide, and nextTick
is a creation solely unto Node.js. The latter two are mechanisms to queue a callback in the short future, such that currently executing JavaScript may continue. If you're used to trying to do this pattern in the browser you may be used to using something like setTimeout(fn, 0)
.
If Node.js actually exposed the idea of a turn of the event loop, you would be expecting the scheduled callback to run at the end of the current loop, or the start of the next -- though from the perspective of your application there isn't really a difference. In