← Back to Node.js Course | Chapter 4: Async Programming | Lesson 7 of 7

process.nextTick

process.nextTick schedules a callback to run right after the current operation, before any other async work.

In this page:

  1. process.nextTick
Syntax
javascript
process.nextTick(() => {
  // runs before promise callbacks and the next event loop phase
});

process.nextTick

nextTick callbacks are processed before promise microtasks and before the event loop moves on. That makes them useful for making sure an emitter's listeners exist before it fires, but overuse can starve I/O. setImmediate runs in the check phase after I/O.

Note: Prefer queueMicrotask or setImmediate in new code unless you need nextTick's ordering.

Example: process.nextTick

javascript
setImmediate(() => console.log("setImmediate"));
Promise.resolve().then(() => console.log("promise then"));
process.nextTick(() => console.log("nextTick"));
console.log("sync");

// Output:
// sync
// nextTick
// promise then
// setImmediate

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Recursively calling nextTick and starving I/O
  2. Confusing nextTick with setImmediate
  3. Expecting nextTick to run after promises
Chapter Summary
  • nextTick runs before promises
  • It runs before the loop continues
  • Overuse starves I/O
  • setImmediate runs after I/O
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.