@@ -15,14 +15,14 @@ or any other asynchronous duration. It is similar to thread-local storage
15
15
in other languages.
16
16
17
17
The ` AsyncLocalStorage ` and ` AsyncResource ` classes are part of the
18
- ` async_hooks ` module:
18
+ ` node: async_hooks` module:
19
19
20
20
``` mjs
21
- import { AsyncLocalStorage , AsyncResource } from ' async_hooks' ;
21
+ import { AsyncLocalStorage , AsyncResource } from ' node: async_hooks' ;
22
22
```
23
23
24
24
``` cjs
25
- const { AsyncLocalStorage , AsyncResource } = require (' async_hooks' );
25
+ const { AsyncLocalStorage , AsyncResource } = require (' node: async_hooks' );
26
26
```
27
27
28
28
## Class: ` AsyncLocalStorage `
@@ -39,18 +39,18 @@ changes:
39
39
40
40
This class creates stores that stay coherent through asynchronous operations.
41
41
42
- While you can create your own implementation on top of the ` async_hooks ` module,
43
- ` AsyncLocalStorage ` should be preferred as it is a performant and memory safe
44
- implementation that involves significant optimizations that are non-obvious to
45
- implement.
42
+ While you can create your own implementation on top of the ` node: async_hooks`
43
+ module, ` AsyncLocalStorage ` should be preferred as it is a performant and memory
44
+ safe implementation that involves significant optimizations that are non-obvious
45
+ to implement.
46
46
47
47
The following example uses ` AsyncLocalStorage ` to build a simple logger
48
48
that assigns IDs to incoming HTTP requests and includes them in messages
49
49
logged within each request.
50
50
51
51
``` mjs
52
- import http from ' http' ;
53
- import { AsyncLocalStorage } from ' async_hooks' ;
52
+ import http from ' node: http' ;
53
+ import { AsyncLocalStorage } from ' node: async_hooks' ;
54
54
55
55
const asyncLocalStorage = new AsyncLocalStorage ();
56
56
@@ -81,8 +81,8 @@ http.get('http://localhost:8080');
81
81
```
82
82
83
83
``` cjs
84
- const http = require (' http' );
85
- const { AsyncLocalStorage } = require (' async_hooks' );
84
+ const http = require (' node: http' );
85
+ const { AsyncLocalStorage } = require (' node: async_hooks' );
86
86
87
87
const asyncLocalStorage = new AsyncLocalStorage ();
88
88
@@ -348,7 +348,7 @@ The `init` hook will trigger when an `AsyncResource` is instantiated.
348
348
The following is an overview of the ` AsyncResource ` API.
349
349
350
350
``` mjs
351
- import { AsyncResource , executionAsyncId } from ' async_hooks' ;
351
+ import { AsyncResource , executionAsyncId } from ' node: async_hooks' ;
352
352
353
353
// AsyncResource() is meant to be extended. Instantiating a
354
354
// new AsyncResource() also triggers init. If triggerAsyncId is omitted then
@@ -376,7 +376,7 @@ asyncResource.triggerAsyncId();
376
376
```
377
377
378
378
``` cjs
379
- const { AsyncResource , executionAsyncId } = require (' async_hooks' );
379
+ const { AsyncResource , executionAsyncId } = require (' node: async_hooks' );
380
380
381
381
// AsyncResource() is meant to be extended. Instantiating a
382
382
// new AsyncResource() also triggers init. If triggerAsyncId is omitted then
@@ -535,14 +535,14 @@ Assuming that the task is adding two numbers, using a file named
535
535
` task_processor.js ` with the following content:
536
536
537
537
``` mjs
538
- import { parentPort } from ' worker_threads' ;
538
+ import { parentPort } from ' node: worker_threads' ;
539
539
parentPort .on (' message' , (task ) => {
540
540
parentPort .postMessage (task .a + task .b );
541
541
});
542
542
```
543
543
544
544
``` cjs
545
- const { parentPort } = require (' worker_threads' );
545
+ const { parentPort } = require (' node: worker_threads' );
546
546
parentPort .on (' message' , (task ) => {
547
547
parentPort .postMessage (task .a + task .b );
548
548
});
@@ -551,10 +551,10 @@ parentPort.on('message', (task) => {
551
551
a Worker pool around it could use the following structure:
552
552
553
553
``` mjs
554
- import { AsyncResource } from ' async_hooks' ;
555
- import { EventEmitter } from ' events' ;
556
- import path from ' path' ;
557
- import { Worker } from ' worker_threads' ;
554
+ import { AsyncResource } from ' node: async_hooks' ;
555
+ import { EventEmitter } from ' node: events' ;
556
+ import path from ' node: path' ;
557
+ import { Worker } from ' node: worker_threads' ;
558
558
559
559
const kTaskInfo = Symbol (' kTaskInfo' );
560
560
const kWorkerFreedEvent = Symbol (' kWorkerFreedEvent' );
@@ -639,10 +639,10 @@ export default class WorkerPool extends EventEmitter {
639
639
` ` `
640
640
641
641
` ` ` cjs
642
- const { AsyncResource } = require (' async_hooks' );
643
- const { EventEmitter } = require (' events' );
644
- const path = require (' path' );
645
- const { Worker } = require (' worker_threads' );
642
+ const { AsyncResource } = require (' node: async_hooks' );
643
+ const { EventEmitter } = require (' node: events' );
644
+ const path = require (' node: path' );
645
+ const { Worker } = require (' node: worker_threads' );
646
646
647
647
const kTaskInfo = Symbol (' kTaskInfo' );
648
648
const kWorkerFreedEvent = Symbol (' kWorkerFreedEvent' );
@@ -738,7 +738,7 @@ This pool could be used as follows:
738
738
739
739
` ` ` mjs
740
740
import WorkerPool from ' ./worker_pool.js' ;
741
- import os from ' os' ;
741
+ import os from ' node: os' ;
742
742
743
743
const pool = new WorkerPool (os .cpus ().length );
744
744
@@ -754,7 +754,7 @@ for (let i = 0; i < 10; i++) {
754
754
755
755
` ` ` cjs
756
756
const WorkerPool = require (' ./worker_pool.js' );
757
- const os = require (' os' );
757
+ const os = require (' node: os' );
758
758
759
759
const pool = new WorkerPool (os .cpus ().length );
760
760
@@ -779,8 +779,8 @@ associate an event listener with the correct execution context. The same
779
779
approach can be applied to a [` Stream` ][] or a similar event-driven class.
780
780
781
781
` ` ` mjs
782
- import { createServer } from ' http' ;
783
- import { AsyncResource , executionAsyncId } from ' async_hooks' ;
782
+ import { createServer } from ' node: http' ;
783
+ import { AsyncResource , executionAsyncId } from ' node: async_hooks' ;
784
784
785
785
const server = createServer ((req , res ) => {
786
786
req .on (' close' , AsyncResource .bind (() => {
@@ -794,8 +794,8 @@ const server = createServer((req, res) => {
794
794
` ` `
795
795
796
796
` ` ` cjs
797
- const { createServer } = require (' http' );
798
- const { AsyncResource , executionAsyncId } = require (' async_hooks' );
797
+ const { createServer } = require (' node: http' );
798
+ const { AsyncResource , executionAsyncId } = require (' node: async_hooks' );
799
799
800
800
const server = createServer ((req , res ) => {
801
801
req .on (' close' , AsyncResource .bind (() => {
0 commit comments