Skip to content

Commit ee6c467

Browse files
committed
doc: recommend queueMicrotask over process.nextTick
We likely cannot ever deprecate process.nextTick, but we can start steering people towards queueMicrotask for most cases. Signed-off-by: James M Snell <[email protected]> Fixes: #36870 PR-URL: #37484 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
1 parent 43e2941 commit ee6c467

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

doc/api/process.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,70 @@ function definitelyAsync(arg, cb) {
17301730
}
17311731
```
17321732

1733+
### When to use `queueMicrotask()` vs. `process.nextTick()`
1734+
1735+
The [`queueMicrotask()`][] API is an alternative to `process.nextTick()` that
1736+
also defers execution of a function using the same microtask queue used to
1737+
execute the then, catch, and finally handlers of resolved promises. Within
1738+
Node.js, every time the "next tick queue" is drained, the microtask queue
1739+
is drained immediately after.
1740+
1741+
```js
1742+
Promise.resolve().then(() => console.log(2));
1743+
queueMicrotask(() => console.log(3));
1744+
process.nextTick(() => console.log(1));
1745+
// Output:
1746+
// 1
1747+
// 2
1748+
// 3
1749+
```
1750+
1751+
For *most* userland use cases, the `queueMicrotask()` API provides a portable
1752+
and reliable mechanism for deferring execution that works across multiple
1753+
JavaScript platform environments and should be favored over `process.nextTick()`.
1754+
In simple scenarios, `queueMicrotask()` can be a drop-in replacement for
1755+
`process.nextTick()`.
1756+
1757+
```js
1758+
console.log('start');
1759+
queueMicrotask(() => {
1760+
console.log('microtask callback');
1761+
});
1762+
console.log('scheduled');
1763+
// Output:
1764+
// start
1765+
// scheduled
1766+
// microtask callback
1767+
```
1768+
1769+
One note-worthy difference between the two APIs is that `process.nextTick()`
1770+
allows specifying additional values that will be passed as arguments to the
1771+
deferred function when it is called. Achieving the same result with
1772+
`queueMicrotask()` requires using either a closure or a bound function:
1773+
1774+
```js
1775+
function deferred(a, b) {
1776+
console.log('microtask', a + b);
1777+
}
1778+
1779+
console.log('start');
1780+
queueMicrotask(deferred.bind(undefined, 1, 2));
1781+
console.log('scheduled');
1782+
// Output:
1783+
// start
1784+
// scheduled
1785+
// microtask 3
1786+
```
1787+
1788+
There are minor differences in the way errors raised from within the next tick
1789+
queue and microtask queue are handled. Errors thrown within a queued microtask
1790+
callback should be handled within the queued callback when possible. If they are
1791+
not, the `process.on('uncaughtException')` event handler can be used to capture
1792+
and handle the errors.
1793+
1794+
When in doubt, unless the specific capabilities of `process.nextTick()` are
1795+
needed, use `queueMicrotask()`.
1796+
17331797
## `process.noDeprecation`
17341798
<!-- YAML
17351799
added: v0.8.0
@@ -2720,6 +2784,7 @@ cases:
27202784
[`process.kill()`]: #process_process_kill_pid_signal
27212785
[`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn
27222786
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
2787+
[`queueMicrotask()`]: globals.md#globals_queuemicrotask_callback
27232788
[`readable.read()`]: stream.md#stream_readable_read_size
27242789
[`require()`]: globals.md#globals_require
27252790
[`require.main`]: modules.md#modules_accessing_the_main_module

0 commit comments

Comments
 (0)