Skip to content

Commit bd5ab9c

Browse files
committed
doc: Explain process.nextTick timing
Provide more detailed explanation of the timing of `process.nextTick` relative to I/O.
1 parent be940b4 commit bd5ab9c

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

doc/api/process.markdown

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,24 @@ This will generate:
454454

455455
## process.nextTick(callback)
456456

457-
On the next loop around the event loop call this callback.
457+
* `callback` {Function}
458+
459+
Once the current event loop turn runs to completion, call the callback
460+
function.
461+
458462
This is *not* a simple alias to `setTimeout(fn, 0)`, it's much more
459-
efficient. It typically runs before any other I/O events fire, but there
460-
are some exceptions.
463+
efficient. It runs before any additional I/O events (including
464+
timers) fire in subsequent ticks of the event loop.
461465

466+
console.log('start');
462467
process.nextTick(function() {
463468
console.log('nextTick callback');
464469
});
470+
console.log('scheduled');
471+
// Output:
472+
// start
473+
// scheduled
474+
// nextTick callback
465475

466476
This is important in developing APIs where you want to give the user the
467477
chance to assign event handlers after an object has been constructed,
@@ -513,6 +523,11 @@ This approach is much better:
513523
fs.stat('file', cb);
514524
}
515525

526+
Note: the nextTick queue is completely drained on each pass of the
527+
event loop **before** additional I/O is processed. As a result,
528+
recursively setting nextTick callbacks will block any I/O from
529+
happening, just like a `while(true);` loop.
530+
516531
## process.umask([mask])
517532

518533
Sets or reads the process's file mode creation mask. Child processes inherit

0 commit comments

Comments
 (0)