Skip to content

Commit 4a40759

Browse files
jasnelltargos
authored andcommitted
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 <jasnell@gmail.com> Fixes: #36870 PR-URL: #37484 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent e1045f1 commit 4a40759

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

doc/api/process.md

+65
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,70 @@ function definitelyAsync(arg, cb) {
17221722
}
17231723
```
17241724

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

0 commit comments

Comments
 (0)