@@ -18,11 +18,9 @@ Experimental
1818The ` node:async_hooks ` module provides an API to track asynchronous resources.
1919It can be accessed using:
2020
21- ``` mjs
21+ ``` mjs|cjs
2222import async_hooks from 'node:async_hooks';
23- ```
24-
25- ``` cjs
23+ --------------
2624const async_hooks = require('node:async_hooks');
2725```
2826
@@ -42,7 +40,7 @@ interface, and each thread will use a new set of async IDs.
4240
4341Following is a simple overview of the public API.
4442
45- ``` mjs
43+ ``` mjs|cjs
4644import async_hooks from 'node:async_hooks';
4745
4846// Return the ID of the current execution context.
@@ -88,9 +86,7 @@ function destroy(asyncId) { }
8886// resolve() function passed to the Promise constructor is invoked
8987// (either directly or through other means of resolving a promise).
9088function promiseResolve(asyncId) { }
91- ```
92-
93- ``` cjs
89+ --------------
9490const async_hooks = require('node:async_hooks');
9591
9692// Return the ID of the current execution context.
@@ -161,16 +157,14 @@ be tracked, then only the `destroy` callback needs to be passed. The
161157specifics of all functions that can be passed to ` callbacks ` is in the
162158[ Hook Callbacks] [ ] section.
163159
164- ``` mjs
160+ ``` mjs|cjs
165161import { createHook } from 'node:async_hooks';
166162
167163const asyncHook = createHook({
168164 init(asyncId, type, triggerAsyncId, resource) { },
169165 destroy(asyncId) { }
170166});
171- ```
172-
173- ``` cjs
167+ --------------
174168const async_hooks = require('node:async_hooks');
175169
176170const asyncHook = async_hooks.createHook({
@@ -226,17 +220,15 @@ synchronous logging operation such as `fs.writeFileSync(file, msg, flag)`.
226220This will print to the file and will not invoke ` AsyncHook ` recursively because
227221it is synchronous.
228222
229- ``` mjs
223+ ``` mjs|cjs
230224import { writeFileSync } from 'node:fs';
231225import { format } from 'node:util';
232226
233227function debug(...args) {
234228 // Use a function like this one when debugging inside an AsyncHook callback
235229 writeFileSync('log.out', `${format(...args)}\n`, { flag: 'a' });
236230}
237- ```
238-
239- ``` cjs
231+ --------------
240232const fs = require('node:fs');
241233const util = require('node:util');
242234
@@ -267,13 +259,11 @@ provided, enabling is a no-op.
267259The ` AsyncHook ` instance is disabled by default. If the ` AsyncHook ` instance
268260should be enabled immediately after creation, the following pattern can be used.
269261
270- ``` mjs
262+ ``` mjs|cjs
271263import { createHook } from 'node:async_hooks';
272264
273265const hook = createHook(callbacks).enable();
274- ```
275-
276- ``` cjs
266+ --------------
277267const async_hooks = require('node:async_hooks');
278268
279269const hook = async_hooks.createHook(callbacks).enable();
@@ -313,15 +303,13 @@ This behavior can be observed by doing something like opening a resource then
313303closing it before the resource can be used. The following snippet demonstrates
314304this.
315305
316- ``` mjs
306+ ``` mjs|cjs
317307import { createServer } from 'node:net';
318308
319309createServer().listen(function() { this.close(); });
320310// OR
321311clearTimeout(setTimeout(() => {}, 10));
322- ```
323-
324- ``` cjs
312+ --------------
325313require('node:net').createServer().listen(function() { this.close(); });
326314// OR
327315clearTimeout(setTimeout(() => {}, 10));
@@ -367,7 +355,7 @@ created, while `triggerAsyncId` shows _why_ a resource was created.
367355
368356The following is a simple demonstration of ` triggerAsyncId ` :
369357
370- ``` mjs
358+ ``` mjs|cjs
371359import { createHook, executionAsyncId } from 'node:async_hooks';
372360import { stdout } from 'node:process';
373361import net from 'node:net';
@@ -382,9 +370,7 @@ createHook({
382370}).enable();
383371
384372net.createServer((conn) => {}).listen(8080);
385- ```
386-
387- ``` cjs
373+ --------------
388374const { createHook, executionAsyncId } = require('node:async_hooks');
389375const { stdout } = require('node:process');
390376const net = require('node:net');
@@ -619,17 +605,15 @@ Using `executionAsyncResource()` in the top-level execution context will
619605return an empty object as there is no handle or request object to use,
620606but having an object representing the top-level can be helpful.
621607
622- ``` mjs
608+ ``` mjs|cjs
623609import { open } from 'node:fs';
624610import { executionAsyncId, executionAsyncResource } from 'node:async_hooks';
625611
626612console.log(executionAsyncId(), executionAsyncResource()); // 1 {}
627613open(new URL(import.meta.url), 'r', (err, fd) => {
628614 console.log(executionAsyncId(), executionAsyncResource()); // 7 FSReqWrap
629615});
630- ` ` `
631-
632- ` ` ` cjs
616+ --------------
633617const { open } = require('node:fs');
634618const { executionAsyncId, executionAsyncResource } = require('node:async_hooks');
635619
@@ -642,7 +626,7 @@ open(__filename, 'r', (err, fd) => {
642626This can be used to implement continuation local storage without the
643627use of a tracking ` Map ` to store the metadata:
644628
645- ` ` ` mjs
629+ ``` mjs|cjs
646630import { createServer } from 'node:http';
647631import {
648632 executionAsyncId,
@@ -666,9 +650,7 @@ const server = createServer((req, res) => {
666650 res.end(JSON.stringify(executionAsyncResource()[sym]));
667651 }, 100);
668652}).listen(3000);
669- ` ` `
670-
671- ` ` ` cjs
653+ --------------
672654const { createServer } = require('node:http');
673655const {
674656 executionAsyncId,
@@ -701,16 +683,14 @@ const server = createServer((req, res) => {
701683* Returns: [ ` number ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type ) The ` asyncId ` of the current execution context. Useful to
702684 track when something calls.
703685
704- ` ` ` mjs
686+ ``` mjs|cjs
705687import { executionAsyncId } from 'node:async_hooks';
706688
707689console.log(executionAsyncId()); // 1 - bootstrap
708690fs.open(path, 'r', (err, fd) => {
709691 console.log(executionAsyncId()); // 6 - open()
710692});
711- ` ` `
712-
713- ` ` ` cjs
693+ --------------
714694const async_hooks = require('node:async_hooks');
715695
716696console.log(async_hooks.executionAsyncId()); // 1 - bootstrap
@@ -778,17 +758,15 @@ expensive nature of the [promise introspection API][PromiseHooks] provided by
778758V8. This means that programs using promises or ` async ` /` await ` will not get
779759correct execution and trigger ids for promise callback contexts by default.
780760
781- ` ` ` mjs
761+ ``` mjs|cjs
782762import { executionAsyncId, triggerAsyncId } from 'node:async_hooks';
783763
784764Promise.resolve(1729).then(() => {
785765 console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`);
786766});
787767// produces:
788768// eid 1 tid 0
789- ` ` `
790-
791- ` ` ` cjs
769+ --------------
792770const { executionAsyncId, triggerAsyncId } = require('node:async_hooks');
793771
794772Promise.resolve(1729).then(() => {
@@ -806,17 +784,15 @@ the resource that caused (triggered) the `then()` callback to be executed.
806784Installing async hooks via ` async_hooks.createHook ` enables promise execution
807785tracking:
808786
809- ` ` ` mjs
787+ ``` mjs|cjs
810788import { createHook, executionAsyncId, triggerAsyncId } from 'node:async_hooks';
811789createHook({ init() {} }).enable(); // forces PromiseHooks to be enabled.
812790Promise.resolve(1729).then(() => {
813791 console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`);
814792});
815793// produces:
816794// eid 7 tid 6
817- ` ` `
818-
819- ` ` ` cjs
795+ --------------
820796const { createHook, executionAsyncId, triggerAsyncId } = require('node:async_hooks');
821797
822798createHook({ init() {} }).enable(); // forces PromiseHooks to be enabled.
0 commit comments