process.nextTick() vs setImmediate()

process.nextTick()

Executes callback after current operation completes, before event loop continues.

console.log('Start');

process.nextTick(() => {
  console.log('nextTick');
});

console.log('End');

// Output:
// Start
// End
// nextTick

setImmediate()

Executes callback in check phase of event loop.

console.log('Start');

setImmediate(() => {
  console.log('setImmediate');
});

console.log('End');

// Output:
// Start
// End
// setImmediate

Key Differences

Featureprocess.nextTick()setImmediate()
WhenAfter current operationCheck phase of event loop
PriorityHigherLower
PhaseBefore event loopIn event loop
Use CaseEmit events, cleanupI/O operations

Execution Order

console.log('1');

setTimeout(() => console.log('2'), 0);

setImmediate(() => console.log('3'));

process.nextTick(() => console.log('4'));

Promise.resolve().then(() => console.log('5'));

console.log('6');

// Output:
// 1
// 6
// 4
// 5
// 2
// 3

In I/O Cycle

const fs = require('fs');

fs.readFile('file.txt', () => {
  setTimeout(() => console.log('setTimeout'), 0);
  setImmediate(() => console.log('setImmediate'));
});

// Output (always):
// setImmediate
// setTimeout

Recursive nextTick (Dangerous)

// BAD - Blocks event loop
process.nextTick(function foo() {
  process.nextTick(foo);
});

// GOOD - Allows event loop to continue
setImmediate(function foo() {
  setImmediate(foo);
});

Use Cases

process.nextTick()

  • Emit events after construction
  • Cleanup operations
  • Error handling

setImmediate()

  • Break up long operations
  • I/O operations
  • Prevent blocking

Interview Tips

  • Explain timing: nextTick before event loop, setImmediate in check phase
  • Show execution order: nextTick has higher priority
  • Discuss use cases: When to use each
  • Mention dangers: Recursive nextTick blocks event loop

Summary

process.nextTick() executes before event loop continues (higher priority). setImmediate() executes in check phase of event loop. Use nextTick for immediate execution, setImmediate to avoid blocking event loop.

Test Your Knowledge

Take a quick quiz to test your understanding of this topic.

Test Your Node.js Knowledge

Ready to put your skills to the test? Take our interactive Node.js quiz and get instant feedback on your answers.