77<!-- source_link=lib/async_hooks.js -->
88
99<!-- YAML
10- llm_description : >
10+ llmDescription : >
1111 Tracks asynchronous execution context. Enables storing and propagating state
1212 across async operations like callbacks and promises. Useful for request
1313 tracking, logging, or context management. Provides AsyncLocalStorage for
@@ -68,16 +68,18 @@ function logWithId(msg) {
6868}
6969
7070let idSeq = 0 ;
71- http .createServer ((req , res ) => {
72- asyncLocalStorage .run (idSeq++ , () => {
73- logWithId (' start' );
74- // Imagine any chain of async operations here
75- setImmediate (() => {
76- logWithId (' finish' );
77- res .end ();
71+ http
72+ .createServer ((req , res ) => {
73+ asyncLocalStorage .run (idSeq++ , () => {
74+ logWithId (' start' );
75+ // Imagine any chain of async operations here
76+ setImmediate (() => {
77+ logWithId (' finish' );
78+ res .end ();
79+ });
7880 });
79- });
80- }) .listen (8080 );
81+ })
82+ .listen (8080 );
8183
8284http .get (' http://localhost:8080' );
8385http .get (' http://localhost:8080' );
@@ -100,16 +102,18 @@ function logWithId(msg) {
100102}
101103
102104let idSeq = 0 ;
103- http .createServer ((req , res ) => {
104- asyncLocalStorage .run (idSeq++ , () => {
105- logWithId (' start' );
106- // Imagine any chain of async operations here
107- setImmediate (() => {
108- logWithId (' finish' );
109- res .end ();
105+ http
106+ .createServer ((req , res ) => {
107+ asyncLocalStorage .run (idSeq++ , () => {
108+ logWithId (' start' );
109+ // Imagine any chain of async operations here
110+ setImmediate (() => {
111+ logWithId (' finish' );
112+ res .end ();
113+ });
110114 });
111- });
112- }) .listen (8080 );
115+ })
116+ .listen (8080 );
113117
114118http .get (' http://localhost:8080' );
115119http .get (' http://localhost:8080' );
@@ -196,9 +200,13 @@ calls the function passed to it within the captured context.
196200
197201``` js
198202const asyncLocalStorage = new AsyncLocalStorage ();
199- const runInAsyncScope = asyncLocalStorage .run (123 , () => AsyncLocalStorage .snapshot ());
200- const result = asyncLocalStorage .run (321 , () => runInAsyncScope (() => asyncLocalStorage .getStore ()));
201- console .log (result); // returns 123
203+ const runInAsyncScope = asyncLocalStorage .run (123 , () =>
204+ AsyncLocalStorage .snapshot ()
205+ );
206+ const result = asyncLocalStorage .run (321 , () =>
207+ runInAsyncScope (() => asyncLocalStorage .getStore ())
208+ );
209+ console .log (result); // returns 123
202210```
203211
204212AsyncLocalStorage.snapshot() can replace the use of AsyncResource for simple
@@ -208,7 +216,9 @@ async context tracking purposes, for example:
208216class Foo {
209217 #runInAsyncScope = AsyncLocalStorage .snapshot ();
210218
211- get () { return this .#runInAsyncScope (() => asyncLocalStorage .getStore ()); }
219+ get () {
220+ return this .#runInAsyncScope (() => asyncLocalStorage .getStore ());
221+ }
212222}
213223
214224const foo = asyncLocalStorage .run (123 , () => new Foo ());
@@ -451,9 +461,10 @@ import { AsyncResource, executionAsyncId } from 'node:async_hooks';
451461// AsyncResource() is meant to be extended. Instantiating a
452462// new AsyncResource() also triggers init. If triggerAsyncId is omitted then
453463// async_hook.executionAsyncId() is used.
454- const asyncResource = new AsyncResource (
455- type, { triggerAsyncId: executionAsyncId (), requireManualDestroy: false },
456- );
464+ const asyncResource = new AsyncResource (type, {
465+ triggerAsyncId: executionAsyncId (),
466+ requireManualDestroy: false ,
467+ });
457468
458469// Run a function in the execution context of the resource. This will
459470// * establish the context of the resource
@@ -479,9 +490,10 @@ const { AsyncResource, executionAsyncId } = require('node:async_hooks');
479490// AsyncResource() is meant to be extended. Instantiating a
480491// new AsyncResource() also triggers init. If triggerAsyncId is omitted then
481492// async_hook.executionAsyncId() is used.
482- const asyncResource = new AsyncResource (
483- type, { triggerAsyncId: executionAsyncId (), requireManualDestroy: false },
484- );
493+ const asyncResource = new AsyncResource (type, {
494+ triggerAsyncId: executionAsyncId (),
495+ requireManualDestroy: false ,
496+ });
485497
486498// Run a function in the execution context of the resource. This will
487499// * establish the context of the resource
@@ -672,7 +684,7 @@ class WorkerPoolTaskInfo extends AsyncResource {
672684
673685 done (err , result ) {
674686 this .runInAsyncScope (this .callback , null , err, result);
675- this .emitDestroy (); // `TaskInfo`s are used only once.
687+ this .emitDestroy (); // `TaskInfo`s are used only once.
676688 }
677689}
678690
@@ -684,8 +696,7 @@ export default class WorkerPool extends EventEmitter {
684696 this .freeWorkers = [];
685697 this .tasks = [];
686698
687- for (let i = 0 ; i < numThreads; i++ )
688- this .addNewWorker ();
699+ for (let i = 0 ; i < numThreads; i++ ) this .addNewWorker ();
689700
690701 // Any time the kWorkerFreedEvent is emitted, dispatch
691702 // the next task pending in the queue, if any.
@@ -711,10 +722,8 @@ export default class WorkerPool extends EventEmitter {
711722 worker .on (' error' , (err ) => {
712723 // In case of an uncaught exception: Call the callback that was passed to
713724 // `runTask` with the error.
714- if (worker[kTaskInfo])
715- worker[kTaskInfo].done (err, null );
716- else
717- this .emit (' error' , err);
725+ if (worker[kTaskInfo]) worker[kTaskInfo].done (err, null );
726+ else this .emit (' error' , err);
718727 // Remove the worker from the list and start a new Worker to replace the
719728 // current one.
720729 this .workers .splice (this .workers .indexOf (worker), 1 );
@@ -760,7 +769,7 @@ class WorkerPoolTaskInfo extends AsyncResource {
760769
761770 done (err , result ) {
762771 this .runInAsyncScope (this .callback , null , err, result);
763- this .emitDestroy (); // `TaskInfo`s are used only once.
772+ this .emitDestroy (); // `TaskInfo`s are used only once.
764773 }
765774}
766775
@@ -772,8 +781,7 @@ class WorkerPool extends EventEmitter {
772781 this .freeWorkers = [];
773782 this .tasks = [];
774783
775- for (let i = 0 ; i < numThreads; i++ )
776- this .addNewWorker ();
784+ for (let i = 0 ; i < numThreads; i++ ) this .addNewWorker ();
777785
778786 // Any time the kWorkerFreedEvent is emitted, dispatch
779787 // the next task pending in the queue, if any.
@@ -799,10 +807,8 @@ class WorkerPool extends EventEmitter {
799807 worker .on (' error' , (err ) => {
800808 // In case of an uncaught exception: Call the callback that was passed to
801809 // `runTask` with the error.
802- if (worker[kTaskInfo])
803- worker[kTaskInfo].done (err, null );
804- else
805- this .emit (' error' , err);
810+ if (worker[kTaskInfo]) worker[kTaskInfo].done (err, null );
811+ else this .emit (' error' , err);
806812 // Remove the worker from the list and start a new Worker to replace the
807813 // current one.
808814 this .workers .splice (this .workers .indexOf (worker), 1 );
@@ -851,8 +857,7 @@ let finished = 0;
851857for (let i = 0 ; i < 10 ; i++ ) {
852858 pool .runTask ({ a: 42 , b: 100 }, (err , result ) => {
853859 console .log (i, err, result);
854- if (++ finished === 10 )
855- pool .close ();
860+ if (++ finished === 10 ) pool .close ();
856861 });
857862}
858863` ` `
@@ -867,8 +872,7 @@ let finished = 0;
867872for (let i = 0 ; i < 10 ; i++ ) {
868873 pool .runTask ({ a: 42 , b: 100 }, (err , result ) => {
869874 console .log (i, err, result);
870- if (++ finished === 10 )
871- pool .close ();
875+ if (++ finished === 10 ) pool .close ();
872876 });
873877}
874878` ` `
@@ -888,9 +892,12 @@ import { createServer } from 'node:http';
888892import { AsyncResource , executionAsyncId } from ' node:async_hooks' ;
889893
890894const server = createServer ((req , res ) => {
891- req .on (' close' , AsyncResource .bind (() => {
892- // Execution context is bound to the current outer scope.
893- }));
895+ req .on (
896+ ' close' ,
897+ AsyncResource .bind (() => {
898+ // Execution context is bound to the current outer scope.
899+ })
900+ );
894901 req .on (' close' , () => {
895902 // Execution context is bound to the scope that caused 'close' to emit.
896903 });
@@ -903,9 +910,12 @@ const { createServer } = require('node:http');
903910const { AsyncResource , executionAsyncId } = require (' node:async_hooks' );
904911
905912const server = createServer ((req , res ) => {
906- req .on (' close' , AsyncResource .bind (() => {
907- // Execution context is bound to the current outer scope.
908- }));
913+ req .on (
914+ ' close' ,
915+ AsyncResource .bind (() => {
916+ // Execution context is bound to the current outer scope.
917+ })
918+ );
909919 req .on (' close' , () => {
910920 // Execution context is bound to the scope that caused 'close' to emit.
911921 });
0 commit comments