| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e1045f1 commit 4a40759
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1722,6 +1722,70 @@ function definitelyAsync(arg, cb) { | |||
| 1722 | 1722 | } | |
| 1723 | 1723 | ``` | |
| 1724 | 1724 | ||
| 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 | + | ||
| 1725 | 1789 | ## `process.noDeprecation` | |
| 1726 | 1790 | <!-- YAML | |
| 1727 | 1791 | added: v0.8.0 | |
@@ -2712,6 +2776,7 @@ cases: | |||
| 2712 | 2776 | [`process.kill()`]: #process_process_kill_pid_signal | |
| 2713 | 2777 | [`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn | |
| 2714 | 2778 | [`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | |
| 2779 | + [`queueMicrotask()`]: globals.md#globals_queuemicrotask_callback | ||
| 2715 | 2780 | [`readable.read()`]: stream.md#stream_readable_read_size | |
| 2716 | 2781 | [`require()`]: globals.md#globals_require | |
| 2717 | 2782 | [`require.main`]: modules.md#modules_accessing_the_main_module | |
| Back | FazBrowse Home | New Git URL |
0 commit comments