Raja Muhammad Asher
Raja Muhammad Asher

Follow

Raja Muhammad Asher

Follow
Differentiate between process.nextTick() and
setImmediate()?

Photo by Max Chen on Unsplash

Differentiate between process.nextTick() and setImmediate()?

Raja Muhammad Asher's photo
Raja Muhammad Asher
·Oct 7, 2022·

1 min read

process.nextTick() and setImmediate() are two ways to schedule a callback function to be executed in the next iteration of the event loop. However, there are some differences between them:

  • process.nextTick() runs the callback before any other I/O events (including timers) that are scheduled for the current iteration of the event loop. This means that process.nextTick() is generally used to perform non-blocking operations that need to be completed as soon as possible.

  • setImmediate() schedules the callback to be run after the current iteration of the event loop is completed. This means that setImmediate() is generally used to schedule non-blocking operations that do not need to be run immediately, but can be run in the next iteration of the event loop.

console.log('Start');

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

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

console.log('End');
Start
End
Inside nextTick
Inside setImmediate
 
Share this