Node.js Reference ยท process
Node.js process.nextTick() method
Reference for process.nextTick() in Node.js: how it jumps the queue ahead of promises and timers, the starvation risk, and when to use queueMicrotask or setImmediate.
process.nextTick(callback) schedules a function to run as soon as the current operation finishes, before the event loop continues to timers, I/O or setImmediate(). It has its own queue, the nextTick queue, which Node empties before it runs promise callbacks.
Its main job is to make an API consistently asynchronous: if a function sometimes calls back at once and sometimes later, callers cannot reason about it. Deferring the immediate case with nextTick() fixes that, and gives the caller time to attach listeners.
process.nextTick(() => console.log('soon')); console.log('first');
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
process.nextTick(callback[, ...args])
| Name | Description |
|---|---|
callback |
The function to run. |
...args |
Optional values passed to callback when it runs. |
Returns: undefined.
Errors / edge cases: a callback that is not a function throws ERR_INVALID_ARG_TYPE immediately. An exception inside the callback becomes an uncaught exception.
Usage notes
- Order: synchronous code finishes first, then all queued
nextTickcallbacks, then promise microtasks, then the event loop proceeds (timers, I/O,setImmediate). In ES modules the first run of top-level code is itself inside a promise job, so there promise callbacks may run beforenextTickcallbacks; the first example uses// cjsto show the classic order. - Starvation: Node empties the whole nextTick queue before moving on. A callback that schedules another
nextTickforever blocks timers and I/O. Never recurse with it for long work. - Prefer
queueMicrotask()for new code that only needs "after this synchronous code". It follows standard browser semantics. ChoosenextTick()when you need to run before promise callbacks, mostly in library internals. - Use
setImmediate()to let I/O callbacks run first, for example when splitting a long loop into chunks. - Consistency rule: a callback API must be either always sync or always async. If one branch of your function calls back immediately, defer it.
- Errors: wrap the callback body when you can; there is no promise to catch them.
- Performance: it is cheap, but an unbounded chain makes the event loop appear frozen even though the process is busy.
- Arguments can be passed directly, which avoids creating a closure.
Related
queueMicrotask(),setImmediate(),setTimeout()- Lessons: Event loop, Events