I'm very confused about the differences between nextTick and setImmediate. I've read all the documentation about them on the internet but I still don't understand how they work.
Examples:
function log(n) { console.log(n); }
setImmediate
setImmediate(function() { setImmediate(function() { log(1); setImmediate(function() { log(2); }); setImmediate(function() { log(3); }); }); setImmediate(function() { log(4); setImmediate(function() { log(5); }); setImmediate(function() { log(6); }); }); }); //1 2 3 4 5 6
nextTick
process.nextTick(function() { process.nextTick(function() { log(1); process.nextTick(function() { log(2); }); process.nextTick(function() { log(3); }); }); process.nextTick(function() { log(4); process.nextTick(function() { log(5); }); process.nextTick(function() { log(6); }); }); }); //1 4 2 3 5 6
Why these results? Please explain with a visual or very easy to follow explanation. Even the node core devs don't agree at how nextTick and setImmediate should be understood by people.
Sources:
nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. setImmediate() method is used to execute a function right after the current event loop finishes. 2.
setImmediate() is designed to execute a script once the current poll phase completes. setTimeout() schedules a script to be run after a minimum threshold in ms has elapsed.
Use nextTick() when you want to make sure that in the next event loop iteration that code is already executed.
The setImmediate function is used to execute a function right after the current event loop finishes. In simple terms, the function functionToExecute is called after all the statements in the script are executed. It is the same as calling the setTimeout function with zero delays.
Consider the following two examples:
setImmediate
setImmediate(function A() { setImmediate(function B() { log(1); setImmediate(function D() { log(2); }); setImmediate(function E() { log(3); }); }); setImmediate(function C() { log(4); setImmediate(function F() { log(5); }); setImmediate(function G() { log(6); }); }); }); setTimeout(function timeout() { console.log('TIMEOUT FIRED'); }, 0) // 'TIMEOUT FIRED' 1 4 2 3 5 6 // OR // 1 'TIMEOUT FIRED' 4 2 3 5 6
nextTick
process.nextTick(function A() { process.nextTick(function B() { log(1); process.nextTick(function D() { log(2); }); process.nextTick(function E() { log(3); }); }); process.nextTick(function C() { log(4); process.nextTick(function F() { log(5); }); process.nextTick(function G() { log(6); }); }); }); setTimeout(function timeout() { console.log('TIMEOUT FIRED'); }, 0) // 1 4 2 3 5 6 'TIMEOUT FIRED'
setImmediate callbacks are fired off the event loop, once per iteration in the order that they were queued. So on the first iteration of the event loop, callback A is fired. Then on the second iteration of the event loop, callback B is fired, then on the third iteration of the event loop callback C is fired, etc. This prevents the event loop from being blocked and allows other I/O or timer callbacks to be called in the mean time (as is the case of the 0ms timeout, which is fired on the 1st or 2nd loop iteration).
nextTick callbacks, however, are always fired immediately after the current code is done executing and BEFORE going back to the event loop. In the nextTick example, we end up executing all the nextTick callbacks before ever returning to the event loop. Since setTimeout's callback will be called from the event loop, the text 'TIMEOUT FIRED' will not be output until we're done with every nextTick callback.
According the Node.js doc names of these two function are exactly swapped
setImmediate() (BEST RECOMMENDED)
It's fire first at event queue
process.nextTick() (USE FOR SPECIAL CASES see example later on)
It's fire immediately, It's kinda write an statement more at the end at the current file
If we have this code
setTimeout(function(){ console.log('Hello world 5'); // It's waiting like a normal person at a queue }, 0); setImmediate(function(){ console.log('Hello world 4'); // It's like get to last and be take care of first // but always after of .nextTick and before of setInterval(, 0) }); process.nextTick(function(){ console.log('Hello world 3'); // It's like be at the bottom at this file }); console.log('Hello world 1'); console.log('Hello world 2');
A visual explanation as per your request:
Cases for use process.nextTick() when you have to emit and event before to handled it:
const EventEmitter = require('events'); const util = require('util'); function MyEmitter() { EventEmitter.call(this); // use nextTick to emit the event once a handler is assigned process.nextTick(function () { this.emit('event'); }.bind(this)); } util.inherits(MyEmitter, EventEmitter); const myEmitter = new MyEmitter(); myEmitter.on('event', function() { console.log('an event occurred!'); });
Look at this vide where Philip Roberts give us a great explanation about runtime event loop and look at this online eventloop debugger Live test how event loop works
Source: https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md#processnexttick-vs-setimmediate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With