process.nextTick
process.nextTick schedules a callback to run right after the current operation, before any other async work.
In this page:
Syntax
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
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
- Recursively calling nextTick and starving I/O
- Confusing nextTick with setImmediate
- 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: