diff options
Diffstat (limited to 'doc/api/async_hooks.md')
-rw-r--r-- | doc/api/async_hooks.md | 76 |
1 files changed, 38 insertions, 38 deletions
diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 4a6af43529..9439edbe22 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -6,15 +6,15 @@ <!-- source_link=lib/async_hooks.js --> -The `async_hooks` module provides an API to track asynchronous resources. It -can be accessed using: +The `node:async_hooks` module provides an API to track asynchronous resources. +It can be accessed using: ```mjs -import async_hooks from 'async_hooks'; +import async_hooks from 'node:async_hooks'; ``` ```cjs -const async_hooks = require('async_hooks'); +const async_hooks = require('node:async_hooks'); ``` ## Terminology @@ -34,7 +34,7 @@ interface, and each thread will use a new set of async IDs. Following is a simple overview of the public API. ```mjs -import async_hooks from 'async_hooks'; +import async_hooks from 'node:async_hooks'; // Return the ID of the current execution context. const eid = async_hooks.executionAsyncId(); @@ -82,7 +82,7 @@ function promiseResolve(asyncId) { } ``` ```cjs -const async_hooks = require('async_hooks'); +const async_hooks = require('node:async_hooks'); // Return the ID of the current execution context. const eid = async_hooks.executionAsyncId(); @@ -155,7 +155,7 @@ specifics of all functions that can be passed to `callbacks` is in the [Hook Callbacks][] section. ```mjs -import { createHook } from 'async_hooks'; +import { createHook } from 'node:async_hooks'; const asyncHook = createHook({ init(asyncId, type, triggerAsyncId, resource) { }, @@ -164,7 +164,7 @@ const asyncHook = createHook({ ``` ```cjs -const async_hooks = require('async_hooks'); +const async_hooks = require('node:async_hooks'); const asyncHook = async_hooks.createHook({ init(asyncId, type, triggerAsyncId, resource) { }, @@ -220,8 +220,8 @@ This will print to the file and will not invoke `AsyncHook` recursively because it is synchronous. ```mjs -import { writeFileSync } from 'fs'; -import { format } from 'util'; +import { writeFileSync } from 'node:fs'; +import { format } from 'node:util'; function debug(...args) { // Use a function like this one when debugging inside an AsyncHook callback @@ -230,8 +230,8 @@ function debug(...args) { ``` ```cjs -const fs = require('fs'); -const util = require('util'); +const fs = require('node:fs'); +const util = require('node:util'); function debug(...args) { // Use a function like this one when debugging inside an AsyncHook callback @@ -261,13 +261,13 @@ The `AsyncHook` instance is disabled by default. If the `AsyncHook` instance should be enabled immediately after creation, the following pattern can be used. ```mjs -import { createHook } from 'async_hooks'; +import { createHook } from 'node:async_hooks'; const hook = createHook(callbacks).enable(); ``` ```cjs -const async_hooks = require('async_hooks'); +const async_hooks = require('node:async_hooks'); const hook = async_hooks.createHook(callbacks).enable(); ``` @@ -307,7 +307,7 @@ closing it before the resource can be used. The following snippet demonstrates this. ```mjs -import { createServer } from 'net'; +import { createServer } from 'node:net'; createServer().listen(function() { this.close(); }); // OR @@ -315,7 +315,7 @@ clearTimeout(setTimeout(() => {}, 10)); ``` ```cjs -require('net').createServer().listen(function() { this.close(); }); +require('node:net').createServer().listen(function() { this.close(); }); // OR clearTimeout(setTimeout(() => {}, 10)); ``` @@ -361,9 +361,9 @@ created, while `triggerAsyncId` shows _why_ a resource was created. The following is a simple demonstration of `triggerAsyncId`: ```mjs -import { createHook, executionAsyncId } from 'async_hooks'; -import { stdout } from 'process'; -import net from 'net'; +import { createHook, executionAsyncId } from 'node:async_hooks'; +import { stdout } from 'node:process'; +import net from 'node:net'; createHook({ init(asyncId, type, triggerAsyncId) { @@ -378,9 +378,9 @@ net.createServer((conn) => {}).listen(8080); ``` ```cjs -const { createHook, executionAsyncId } = require('async_hooks'); -const { stdout } = require('process'); -const net = require('net'); +const { createHook, executionAsyncId } = require('node:async_hooks'); +const { stdout } = require('node:process'); +const net = require('node:net'); createHook({ init(asyncId, type, triggerAsyncId) { @@ -433,9 +433,9 @@ callback to `listen()` will look like. The output formatting is slightly more elaborate to make calling context easier to see. ```js -const async_hooks = require('async_hooks'); -const fs = require('fs'); -const net = require('net'); +const async_hooks = require('node:async_hooks'); +const fs = require('node:fs'); +const net = require('node:net'); const { fd } = process.stdout; let indent = 0; @@ -619,8 +619,8 @@ return an empty object as there is no handle or request object to use, but having an object representing the top-level can be helpful. ```mjs -import { open } from 'fs'; -import { executionAsyncId, executionAsyncResource } from 'async_hooks'; +import { open } from 'node:fs'; +import { executionAsyncId, executionAsyncResource } from 'node:async_hooks'; console.log(executionAsyncId(), executionAsyncResource()); // 1 {} open(new URL(import.meta.url), 'r', (err, fd) => { @@ -629,8 +629,8 @@ open(new URL(import.meta.url), 'r', (err, fd) => { ``` ```cjs -const { open } = require('fs'); -const { executionAsyncId, executionAsyncResource } = require('async_hooks'); +const { open } = require('node:fs'); +const { executionAsyncId, executionAsyncResource } = require('node:async_hooks'); console.log(executionAsyncId(), executionAsyncResource()); // 1 {} open(__filename, 'r', (err, fd) => { @@ -642,7 +642,7 @@ This can be used to implement continuation local storage without the use of a tracking `Map` to store the metadata: ```mjs -import { createServer } from 'http'; +import { createServer } from 'node:http'; import { executionAsyncId, executionAsyncResource, @@ -668,12 +668,12 @@ const server = createServer((req, res) => { ``` ```cjs -const { createServer } = require('http'); +const { createServer } = require('node:http'); const { executionAsyncId, executionAsyncResource, createHook -} = require('async_hooks'); +} = require('node:async_hooks'); const sym = Symbol('state'); // Private symbol to avoid pollution createHook({ @@ -707,7 +707,7 @@ changes: track when something calls. ```mjs -import { executionAsyncId } from 'async_hooks'; +import { executionAsyncId } from 'node:async_hooks'; console.log(executionAsyncId()); // 1 - bootstrap fs.open(path, 'r', (err, fd) => { @@ -716,7 +716,7 @@ fs.open(path, 'r', (err, fd) => { ``` ```cjs -const async_hooks = require('async_hooks'); +const async_hooks = require('node:async_hooks'); console.log(async_hooks.executionAsyncId()); // 1 - bootstrap fs.open(path, 'r', (err, fd) => { @@ -788,7 +788,7 @@ V8. This means that programs using promises or `async`/`await` will not get correct execution and trigger ids for promise callback contexts by default. ```mjs -import { executionAsyncId, triggerAsyncId } from 'async_hooks'; +import { executionAsyncId, triggerAsyncId } from 'node:async_hooks'; Promise.resolve(1729).then(() => { console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`); @@ -798,7 +798,7 @@ Promise.resolve(1729).then(() => { ``` ```cjs -const { executionAsyncId, triggerAsyncId } = require('async_hooks'); +const { executionAsyncId, triggerAsyncId } = require('node:async_hooks'); Promise.resolve(1729).then(() => { console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`); @@ -816,7 +816,7 @@ Installing async hooks via `async_hooks.createHook` enables promise execution tracking: ```mjs -import { createHook, executionAsyncId, triggerAsyncId } from 'async_hooks'; +import { createHook, executionAsyncId, triggerAsyncId } from 'node:async_hooks'; createHook({ init() {} }).enable(); // forces PromiseHooks to be enabled. Promise.resolve(1729).then(() => { console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`); @@ -826,7 +826,7 @@ Promise.resolve(1729).then(() => { ``` ```cjs -const { createHook, executionAsyncId, triggerAsyncId } = require('async_hooks'); +const { createHook, executionAsyncId, triggerAsyncId } = require('node:async_hooks'); createHook({ init() {} }).enable(); // forces PromiseHooks to be enabled. Promise.resolve(1729).then(() => { |