@@ -1730,6 +1730,41 @@ 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. The timing of queued microtasks is nearly
1740+ identical to ` process.nextTick() ` with the notable exception that the next
1741+ tick queue always executes first. There are also minor differences in the way
1742+ errors raised from within the next tick queue and microtask queue are handled.
1743+
1744+ For * most* userland use cases, the ` queueMicrotask() ` API provides a portable
1745+ and reliable mechanism for deferring execution that works across multiple
1746+ JavaScript platform environments and should be favored over ` process.nextTick() ` .
1747+ In most scenarios, ` queueMicrotask() ` can be a drop-in replacement for
1748+ ` process.nextTick() ` .
1749+
1750+ When in doubt, use ` queueMicrotask() ` .
1751+
1752+ ``` js
1753+ console .log (' start' );
1754+ queueMicrotask (() => {
1755+ console .log (' microtask callback' );
1756+ });
1757+ console .log (' scheduled' );
1758+ // Output:
1759+ // start
1760+ // scheduled
1761+ // microtask callback
1762+ ```
1763+
1764+ Errors thrown within a queued microtask callback should be handled within the
1765+ queued callback. If they are not, the ` process.on('uncaughtException') ` event
1766+ handler can be used to capture and handle the errors.
1767+
17331768## ` process.noDeprecation `
17341769<!-- YAML
17351770added: v0.8.0
@@ -2720,6 +2755,7 @@ cases:
27202755[ `process.kill()` ] : #process_process_kill_pid_signal
27212756[ `process.setUncaughtExceptionCaptureCallback()` ] : process.md#process_process_setuncaughtexceptioncapturecallback_fn
27222757[ `promise.catch()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
2758+ [ `queueMicrotask()` ] : globals.md#globals_queuemicrotask_callback
27232759[ `readable.read()` ] : stream.md#stream_readable_read_size
27242760[ `require()` ] : globals.md#globals_require
27252761[ `require.main` ] : modules.md#modules_accessing_the_main_module
0 commit comments