nextTick vs setImmediate, visual explanation

node.js

node.js Problem Overview


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:

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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:

enter image description here

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][4]

Source: https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md#processnexttick-vs-setimmediate

[4]: http://latentflip.com/loupe/ "Live explanation"

Solution 3 - node.js

I can't reproduce your results for setImmediate. It should be the same as nextTick (and it is in my tests) since in this situation they do pretty much the same thing. The only reasonable explanation for that is that setImmediate is somehow synchronous, but it is not.

And according to NodeJS documentation the only real difference is that multiple nextTick may fire in one loop iteration (depending on maxTickDepth), while setImmediate fires once per iteration.

Solution 4 - node.js

Below gives you better clarity.

setImmediate

  1. It's execute a script once the current poll phase completes.
  2. It's a timer module function and timer functions are global, you can call them without require.
  3. It can cleared by clearImmediate().
  4. Set "immediate" execution of the callback after I/O events' callbacks before setTimeout() and setInterval().

nextTick

  1. It's a process global object function of NodeJS.
  2. All callbacks passed to process.nextTick() will be resolved before the event loop continues.
  3. Allow users to handle errors.
  4. Helps to try the request again before the event loop continues.
Simple code Snippet.
console.log("I'm First");

setImmediate(function () {
  console.log('Im setImmediate');
});

console.log("I'm Second");

process.nextTick(function () {
  console.log('Im nextTick');
});

console.log("I'm Last");

/*
Output
$ node server.js
I'm First
I'm Second
I'm Last
Im nextTick
Im setImmediate
*/

Solution 5 - node.js

I think all the answers above are obsolete, because I got different answers constantly with the current version of nodejs and it is easy to reason about

var log=console.log
log(process.version)

var makeAsyncCall
if(false)
    makeAsyncCall=setImmediate
else
    makeAsyncCall=process.nextTick;

makeAsyncCall(function A () {
    makeAsyncCall(function B() {
        log(1);
        makeAsyncCall(function C() { log(2); });
        makeAsyncCall(function D() { log(3); });
    });
    makeAsyncCall(function E() {
        log(4);
        makeAsyncCall(function F() { log(5); });
        makeAsyncCall(function G() { log(6); });
    });
});
//1
//4
//2
//3
//5
//6
//in both case

After reading https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md#processnexttick-vs-setimmediate let use start from setImmediate we should keep track of the check queue because it is where the setImmediate callback reside.

First iteration

A is push to check queue

check queue :[A]

Second iteration

A is pull out from queue to execute

During its execution ,it put B and E to queueand then, A complete and start next iteration

check queue :[B,E]

Third iteration

pull out B and push C D

check queue :[E,C,D]

Forth iteration

pull out E and push F G

check queue : [C,D,F,G]

Finally

execute the callbacks in the queue sequentially

For nextTick case, the queue works exactly the same way,that is why it produce same result

The different is that : > the nextTickQueue will be processed after the current operation > completes, regardless of the current phase of the event loop

To be clear,the event loop maintain multiple queues and check queue is just one of them,the node will decide which queue to use based on some rules

with process.nextTick however,it is sort of bypassing all the rule and execute the callback in nextTick immediately

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabriel LlamasView Question on Stackoverflow
Solution 1 - node.jsDave StibranyView Answer on Stackoverflow
Solution 2 - node.jsgsalgadotoledoView Answer on Stackoverflow
Solution 3 - node.jsfreakishView Answer on Stackoverflow
Solution 4 - node.jsVenkat.RView Answer on Stackoverflow
Solution 5 - node.jsGuichiView Answer on Stackoverflow