| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ae360c3 commit 8e978bd
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2993,34 +2993,40 @@ function definitelyAsync(arg, cb) { | |||
| 2993 | 2993 | ||
| 2994 | 2994 | ### When to use `queueMicrotask()` vs. `process.nextTick()` | |
| 2995 | 2995 | ||
| 2996 | - The [`queueMicrotask()`][] API is an alternative to `process.nextTick()` that | ||
| 2997 | - also defers execution of a function using the same microtask queue used to | ||
| 2998 | - execute the then, catch, and finally handlers of resolved promises. Within | ||
| 2999 | - Node.js, every time the "next tick queue" is drained, the microtask queue | ||
| 2996 | + The [`queueMicrotask()`][] API is an alternative to `process.nextTick()` that instead of using the | ||
| 2997 | + "next tick queue" defers execution of a function using the same microtask queue used to execute the | ||
| 2998 | + then, catch, and finally handlers of resolved promises. | ||
| 2999 | + | ||
| 3000 | + Within Node.js, every time the "next tick queue" is drained, the microtask queue | ||
| 3000 | 3001 | is drained immediately after. | |
| 3001 | 3002 | ||
| 3003 | + So in CJS modules `process.nextTick()` callbacks are always run before `queueMicrotask()` ones. | ||
| 3004 | + However since ESM modules are processed already as part of the microtask queue, there | ||
| 3005 | + `queueMicrotask()` callbacks are always exectued before `process.nextTick()` ones since Node.js | ||
| 3006 | + is already in the process of draining the microtask queue. | ||
| 3007 | + | ||
| 3002 | 3008 | ```mjs | |
| 3003 | 3009 | import { nextTick } from 'node:process'; | |
| 3004 | 3010 | ||
| 3005 | - Promise.resolve().then(() => console.log(2)); | ||
| 3006 | - queueMicrotask(() => console.log(3)); | ||
| 3007 | - nextTick(() => console.log(1)); | ||
| 3011 | + Promise.resolve().then(() => console.log('resolve')); | ||
| 3012 | + queueMicrotask(() => console.log('microtask')); | ||
| 3013 | + nextTick(() => console.log('nextTick')); | ||
| 3008 | 3014 | // Output: | |
| 3009 | - // 1 | ||
| 3010 | - // 2 | ||
| 3011 | - // 3 | ||
| 3015 | + // resolve | ||
| 3016 | + // microtask | ||
| 3017 | + // nextTick | ||
| 3012 | 3018 | ``` | |
| 3013 | 3019 | ||
| 3014 | 3020 | ```cjs | |
| 3015 | 3021 | const { nextTick } = require('node:process'); | |
| 3016 | 3022 | ||
| 3017 | - Promise.resolve().then(() => console.log(2)); | ||
| 3018 | - queueMicrotask(() => console.log(3)); | ||
| 3019 | - nextTick(() => console.log(1)); | ||
| 3023 | + Promise.resolve().then(() => console.log('resolve')); | ||
| 3024 | + queueMicrotask(() => console.log('microtask')); | ||
| 3025 | + nextTick(() => console.log('nextTick')); | ||
| 3020 | 3026 | // Output: | |
| 3021 | - // 1 | ||
| 3022 | - // 2 | ||
| 3023 | - // 3 | ||
| 3027 | + // nextTick | ||
| 3028 | + // resolve | ||
| 3029 | + // microtask | ||
| 3024 | 3030 | ``` | |
| 3025 | 3031 | ||
| 3026 | 3032 | For _most_ userland use cases, the `queueMicrotask()` API provides a portable | |
| Back | FazBrowse Home | New Git URL |
0 commit comments