@@ -18,11 +18,9 @@ Experimental
18
18
The ` node:async_hooks ` module provides an API to track asynchronous resources.
19
19
It can be accessed using:
20
20
21
- ``` mjs
21
+ ``` mjs|cjs
22
22
import async_hooks from 'node:async_hooks';
23
- ```
24
-
25
- ``` cjs
23
+ --------------
26
24
const async_hooks = require('node:async_hooks');
27
25
```
28
26
@@ -42,7 +40,7 @@ interface, and each thread will use a new set of async IDs.
42
40
43
41
Following is a simple overview of the public API.
44
42
45
- ``` mjs
43
+ ``` mjs|cjs
46
44
import async_hooks from 'node:async_hooks';
47
45
48
46
// Return the ID of the current execution context.
@@ -88,9 +86,7 @@ function destroy(asyncId) { }
88
86
// resolve() function passed to the Promise constructor is invoked
89
87
// (either directly or through other means of resolving a promise).
90
88
function promiseResolve(asyncId) { }
91
- ```
92
-
93
- ``` cjs
89
+ --------------
94
90
const async_hooks = require('node:async_hooks');
95
91
96
92
// Return the ID of the current execution context.
@@ -161,16 +157,14 @@ be tracked, then only the `destroy` callback needs to be passed. The
161
157
specifics of all functions that can be passed to ` callbacks ` is in the
162
158
[ Hook Callbacks] [ ] section.
163
159
164
- ``` mjs
160
+ ``` mjs|cjs
165
161
import { createHook } from 'node:async_hooks';
166
162
167
163
const asyncHook = createHook({
168
164
init(asyncId, type, triggerAsyncId, resource) { },
169
165
destroy(asyncId) { }
170
166
});
171
- ```
172
-
173
- ``` cjs
167
+ --------------
174
168
const async_hooks = require('node:async_hooks');
175
169
176
170
const asyncHook = async_hooks.createHook({
@@ -226,17 +220,15 @@ synchronous logging operation such as `fs.writeFileSync(file, msg, flag)`.
226
220
This will print to the file and will not invoke ` AsyncHook ` recursively because
227
221
it is synchronous.
228
222
229
- ``` mjs
223
+ ``` mjs|cjs
230
224
import { writeFileSync } from 'node:fs';
231
225
import { format } from 'node:util';
232
226
233
227
function debug(...args) {
234
228
// Use a function like this one when debugging inside an AsyncHook callback
235
229
writeFileSync('log.out', `${format(...args)}\n`, { flag: 'a' });
236
230
}
237
- ```
238
-
239
- ``` cjs
231
+ --------------
240
232
const fs = require('node:fs');
241
233
const util = require('node:util');
242
234
@@ -267,13 +259,11 @@ provided, enabling is a no-op.
267
259
The ` AsyncHook ` instance is disabled by default. If the ` AsyncHook ` instance
268
260
should be enabled immediately after creation, the following pattern can be used.
269
261
270
- ``` mjs
262
+ ``` mjs|cjs
271
263
import { createHook } from 'node:async_hooks';
272
264
273
265
const hook = createHook(callbacks).enable();
274
- ```
275
-
276
- ``` cjs
266
+ --------------
277
267
const async_hooks = require('node:async_hooks');
278
268
279
269
const hook = async_hooks.createHook(callbacks).enable();
@@ -313,15 +303,13 @@ This behavior can be observed by doing something like opening a resource then
313
303
closing it before the resource can be used. The following snippet demonstrates
314
304
this.
315
305
316
- ``` mjs
306
+ ``` mjs|cjs
317
307
import { createServer } from 'node:net';
318
308
319
309
createServer().listen(function() { this.close(); });
320
310
// OR
321
311
clearTimeout(setTimeout(() => {}, 10));
322
- ```
323
-
324
- ``` cjs
312
+ --------------
325
313
require('node:net').createServer().listen(function() { this.close(); });
326
314
// OR
327
315
clearTimeout(setTimeout(() => {}, 10));
@@ -367,7 +355,7 @@ created, while `triggerAsyncId` shows _why_ a resource was created.
367
355
368
356
The following is a simple demonstration of ` triggerAsyncId ` :
369
357
370
- ``` mjs
358
+ ``` mjs|cjs
371
359
import { createHook, executionAsyncId } from 'node:async_hooks';
372
360
import { stdout } from 'node:process';
373
361
import net from 'node:net';
@@ -382,9 +370,7 @@ createHook({
382
370
}).enable();
383
371
384
372
net.createServer((conn) => {}).listen(8080);
385
- ```
386
-
387
- ``` cjs
373
+ --------------
388
374
const { createHook, executionAsyncId } = require('node:async_hooks');
389
375
const { stdout } = require('node:process');
390
376
const net = require('node:net');
@@ -619,17 +605,15 @@ Using `executionAsyncResource()` in the top-level execution context will
619
605
return an empty object as there is no handle or request object to use,
620
606
but having an object representing the top-level can be helpful.
621
607
622
- ``` mjs
608
+ ``` mjs|cjs
623
609
import { open } from 'node:fs';
624
610
import { executionAsyncId, executionAsyncResource } from 'node:async_hooks';
625
611
626
612
console.log(executionAsyncId(), executionAsyncResource()); // 1 {}
627
613
open(new URL(import.meta.url), 'r', (err, fd) => {
628
614
console.log(executionAsyncId(), executionAsyncResource()); // 7 FSReqWrap
629
615
});
630
- ` ` `
631
-
632
- ` ` ` cjs
616
+ --------------
633
617
const { open } = require('node:fs');
634
618
const { executionAsyncId, executionAsyncResource } = require('node:async_hooks');
635
619
@@ -642,7 +626,7 @@ open(__filename, 'r', (err, fd) => {
642
626
This can be used to implement continuation local storage without the
643
627
use of a tracking ` Map ` to store the metadata:
644
628
645
- ` ` ` mjs
629
+ ``` mjs|cjs
646
630
import { createServer } from 'node:http';
647
631
import {
648
632
executionAsyncId,
@@ -666,9 +650,7 @@ const server = createServer((req, res) => {
666
650
res.end(JSON.stringify(executionAsyncResource()[sym]));
667
651
}, 100);
668
652
}).listen(3000);
669
- ` ` `
670
-
671
- ` ` ` cjs
653
+ --------------
672
654
const { createServer } = require('node:http');
673
655
const {
674
656
executionAsyncId,
@@ -701,16 +683,14 @@ const server = createServer((req, res) => {
701
683
* Returns: [ ` number ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type ) The ` asyncId ` of the current execution context. Useful to
702
684
track when something calls.
703
685
704
- ` ` ` mjs
686
+ ``` mjs|cjs
705
687
import { executionAsyncId } from 'node:async_hooks';
706
688
707
689
console.log(executionAsyncId()); // 1 - bootstrap
708
690
fs.open(path, 'r', (err, fd) => {
709
691
console.log(executionAsyncId()); // 6 - open()
710
692
});
711
- ` ` `
712
-
713
- ` ` ` cjs
693
+ --------------
714
694
const async_hooks = require('node:async_hooks');
715
695
716
696
console.log(async_hooks.executionAsyncId()); // 1 - bootstrap
@@ -778,17 +758,15 @@ expensive nature of the [promise introspection API][PromiseHooks] provided by
778
758
V8. This means that programs using promises or ` async ` /` await ` will not get
779
759
correct execution and trigger ids for promise callback contexts by default.
780
760
781
- ` ` ` mjs
761
+ ``` mjs|cjs
782
762
import { executionAsyncId, triggerAsyncId } from 'node:async_hooks';
783
763
784
764
Promise.resolve(1729).then(() => {
785
765
console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`);
786
766
});
787
767
// produces:
788
768
// eid 1 tid 0
789
- ` ` `
790
-
791
- ` ` ` cjs
769
+ --------------
792
770
const { executionAsyncId, triggerAsyncId } = require('node:async_hooks');
793
771
794
772
Promise.resolve(1729).then(() => {
@@ -806,17 +784,15 @@ the resource that caused (triggered) the `then()` callback to be executed.
806
784
Installing async hooks via ` async_hooks.createHook ` enables promise execution
807
785
tracking:
808
786
809
- ` ` ` mjs
787
+ ``` mjs|cjs
810
788
import { createHook, executionAsyncId, triggerAsyncId } from 'node:async_hooks';
811
789
createHook({ init() {} }).enable(); // forces PromiseHooks to be enabled.
812
790
Promise.resolve(1729).then(() => {
813
791
console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`);
814
792
});
815
793
// produces:
816
794
// eid 7 tid 6
817
- ` ` `
818
-
819
- ` ` ` cjs
795
+ --------------
820
796
const { createHook, executionAsyncId, triggerAsyncId } = require('node:async_hooks');
821
797
822
798
createHook({ init() {} }).enable(); // forces PromiseHooks to be enabled.
0 commit comments