@@ -169,7 +169,7 @@ class AsyncLocal<T = any> {
169
169
type ValueChangedListener< T > = (newValue: T , prevValue: T );
170
170
` ` `
171
171
172
- ` AsyncLocal .getValue ()` returns the current value of the context .
172
+ ` AsyncLocal .getValue ()` returns the current value of the async execution flow .
173
173
174
174
As the value of AsyncLocal has to be fetched from its own store, i.e. the
175
175
AsyncLocal object. From arbitrary execution context in an async execution
@@ -179,31 +179,27 @@ propagates along with an async execution flow.
179
179
AsyncLocal propagates values along the logical async execution flow.
180
180
181
181
` ` ` js
182
- const context = new AsyncLocal ();
182
+ const asyncLocal = new AsyncLocal ();
183
183
184
184
(function main () {
185
- context .setValue (' main' );
185
+ asyncLocal .setValue (' main' );
186
186
187
187
setTimeout (() => {
188
- printContext ( ); // => 'main'
189
- context .setValue (' first timer' );
188
+ console . log ( asyncLocal . getValue () ); // => 'main'
189
+ asyncLocal .setValue (' first timer' );
190
190
setTimeout (() => {
191
- printContext ( ); // => 'first timer'
191
+ console . log ( asyncLocal . getValue () ); // => 'first timer'
192
192
}, 1000 );
193
193
}, 1000 );
194
194
195
195
setTimeout (() => {
196
- printContext ( ); // => 'main'
197
- context .setValue (' second timer' );
196
+ console . log ( asyncLocal . getValue () ); // => 'main'
197
+ asyncLocal .setValue (' second timer' );
198
198
setTimeout (() => {
199
- printContext ( ); // => 'second timer'
199
+ console . log ( asyncLocal . getValue () ); // => 'second timer'
200
200
}, 1000 );
201
201
}, 1000 );
202
202
})();
203
-
204
- function printContext () {
205
- console .log (context .getValue ());
206
- }
207
203
` ` `
208
204
209
205
The optional ` valueChangedListener` will be called each time the value in the
@@ -219,7 +215,7 @@ since the code where trigger the listener has to explicitly refer to the
219
215
instance of AsyncLocal.
220
216
221
217
` ` ` js
222
- const context = new AsyncLocal (
218
+ const asyncLocal = new AsyncLocal (
223
219
(newValue , prevValue ) =>
224
220
console .log (` valueChanged: newValue(${ newValue} ), prevValue(${ prevValue} )` )
225
221
);
@@ -230,16 +226,16 @@ Promise.resolve().then(run);
230
226
231
227
async function run () {
232
228
// (1)
233
- context .setValue (' foo' );
229
+ asyncLocal .setValue (' foo' );
234
230
await sleep (1000 );
235
- await next (context );
231
+ await next (asyncLocal );
236
232
// (3)
237
- context .setValue (' quz' );
233
+ asyncLocal .setValue (' quz' );
238
234
}
239
235
240
236
async function next () {
241
237
// (2)
242
- context .setValue (' bar' );
238
+ asyncLocal .setValue (' bar' );
243
239
await sleep (1000 );
244
240
}
245
241
` ` `
@@ -391,8 +387,8 @@ class AsyncTask {
391
387
` ` `
392
388
393
389
` AsyncTask .runInAsyncScope ` calls the provided function with the provided
394
- arguments in the execution context of the async task. This will establish
395
- the context, call the function, and then restore the original async execution
390
+ arguments in the async context of the async task. This will establish
391
+ the async context, call the function, and then restore the original async
396
392
context.
397
393
398
394
### Using ` AsyncTask`
@@ -403,7 +399,7 @@ function connect(port, host) {
403
399
let nextId = 0 ;
404
400
const requestResponseMap = new Map ();
405
401
406
- // Establish the connection, the client is linked to the current context.
402
+ // Establish the connection, the client is linked to the current async context.
407
403
const client = net .createConnection ({ host, port });
408
404
client .on (' connect' , () => {
409
405
console .log (' connected to server' );
@@ -412,9 +408,9 @@ function connect(port, host) {
412
408
console .log (' disconnected from server' );
413
409
});
414
410
415
- // the client is created at the context of `connect`,
416
- // the listeners will be triggered at the context of the
417
- // client initiating context.
411
+ // the client is created at the async context of `connect`,
412
+ // the listeners will be triggered at the async context of the
413
+ // client initiating async context.
418
414
client .on (' data' , (res ) => {
419
415
const { id , data } = JSON .parse (res .toString (' utf8' ));
420
416
const req = requestResponseMap .get (id);
@@ -423,7 +419,7 @@ function connect(port, host) {
423
419
return ;
424
420
}
425
421
426
- // The req.handler callback is called under the context of client
422
+ // The req.handler callback is called under the async context of client
427
423
// listeners.
428
424
req .handler (data);
429
425
});
@@ -439,7 +435,7 @@ function connect(port, host) {
439
435
// AsyncTask & Promise based connection wrapper.
440
436
class DatabaseConnection {
441
437
constructor (port , host ) {
442
- // Initialize connection, possibly in root context.
438
+ // Initialize connection, possibly in root async context.
443
439
this .socket = connect (port, host);
444
440
}
445
441
@@ -470,13 +466,13 @@ class QueryTask extends AsyncTask {
470
466
}
471
467
` ` `
472
468
473
- In the example above, ` DatabaseConnection` can be established at root execution
469
+ In the example above, ` DatabaseConnection` can be established at root async
474
470
context (or any other context). With ` AsyncTask` , each call to
475
471
` DatabaseConnection .query ` will schedule an async task, which will be linked
476
- to its initiator execution context (may not be the one establishing
472
+ to its initiator async context (may not be the one establishing
477
473
` DatabaseConnection` ). And at the resolution of socket, the contexts are
478
474
propagated by the ` DatabaseConnection` , which is linked to its initiating
479
- execution context, so the context has to be re-established by
475
+ async context, so the async context has to be re-established by
480
476
` AsyncTask .runInAsyncScope ` .
481
477
482
478
In this way, we can propagate correct async context flows on multiplexing
0 commit comments