What are the differences between process.nextTick(), setImmediate(), and setTimeout()?

  • process.nextTick() adds a callback to the next tick of the event loop, executed before any I/O operations.
  • setImmediate() schedules a callback after the current event loop cycle completes.
  • setTimeout() schedules a callback after a specified delay, allowing other I/O operations to complete first.

Example:

console.log('Start');

process.nextTick(() => console.log('Next Tick')); // Runs first
setImmediate(() => console.log('Immediate')); // Runs second
setTimeout(() => console.log('Timeout'), 0); // Runs last

console.log('End');