Difference between process.nextTick() and setImmediate() Methods - GeeksforGeeks

11 Apr, 2022
To understand the difference between process.nextTick() and setImmediate() methods, we first need to understand the working of Node.js Event Loop.
What is Node.js Event Loop?Due to the fact that JavaScript is single-threaded i.e. it performs only a single process at a time, so it is the Event Loop that allows Node.js to perform non-blocking I/O operations.
Working of Event Loop?At the start of a JavaScript program, an event loop is initialized. There are several operations that execute in an event loop. Below is the sequence in which different they are executed in a single iteration of the event loop. These operations are processed in a queue.
timers–>pending callbacks–>idle,prepare–>connections(poll,data,etc)–>check–>close callbacks
Understanding process.nextTick() method: Whenever a new queue of operations is initialized we can think of it as a new tick. The process.nextTick() method adds the callback function to the start of the next event queue. It is to be noted that, at the start of the program process.nextTick() method is called for the first time before the event loop is processed.
Syntax:
process.nextTick(callback);
Understanding setImmdeiate() method: Whenever we call setImmediate() method, it’s callback function is placed in the check phase of the next event queue. There is slight detail to be noted here that setImmediate() method is called in the poll phase and it’s callback functions are invoked in the check phase.
Syntax:
setImmediate(callback);
Example:
setImmediate(function A() {    console.log("1st immediate");});  setImmediate(function B() {    console.log("2nd immediate");});  process.nextTick(function C() {    console.log("1st process");});  process.nextTick(function D() {    console.log("2nd process");});  // First event queue ends hereconsole.log("program started");
For the above program, event queues are initialized in the following manner:
In the first event queue only ‘program started is printed’.Then second event queue is started and function C i.e. callback of process.nextTick() method is placed at the start of the event queue. C is executed and the queue ends.Then previous event queue ends and third event queue is initialized with callback D. Then callback function A of setImmdeiate() method is placed in the followed by B.Now, the third event queue looks like this,D A BNow functions D, A, B are executed in the order they are present in the queue.Output:
In the first event queue only ‘program started is printed’.
Then second event queue is started and function C i.e. callback of process.nextTick() method is placed at the start of the event queue. C is executed and the queue ends.
Then previous event queue ends and third event queue is initialized with callback D. Then callback function A of setImmdeiate() method is placed in the followed by B.Now, the third event queue looks like this,D A BNow functions D, A, B are executed in the order they are present in the queue.Output:
D A B
Now functions D, A, B are executed in the order they are present in the queue.
Output:
Let us see the differences in a tabular form -:
Its syntax is -:
process.nextTick(callback);
Its syntax is -:
setImmediate(callback);
mayank007rawa
Node.js-Methods
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript