@@ -29,11 +29,11 @@ Following is a simple overview of the public API.
29
29
const async_hooks = require (' async_hooks' );
30
30
31
31
// Return the ID of the current execution context.
32
- const cid = async_hooks .currentId ();
32
+ const eid = async_hooks .executionAsyncId ();
33
33
34
34
// Return the ID of the handle responsible for triggering the callback of the
35
35
// current execution scope to call.
36
- const tid = async_hooks .triggerId ();
36
+ const tid = async_hooks .triggerAsyncId ();
37
37
38
38
// Create a new AsyncHook instance. All of these callbacks are optional.
39
39
const asyncHook = async_hooks .createHook ({ init, before, after, destroy });
@@ -53,7 +53,7 @@ asyncHook.disable();
53
53
// init is called during object construction. The resource may not have
54
54
// completed construction when this callback runs, therefore all fields of the
55
55
// resource referenced by "asyncId" may not have been populated.
56
- function init (asyncId , type , triggerId , resource ) { }
56
+ function init (asyncId , type , triggerAsyncId , resource ) { }
57
57
58
58
// before is called just before the resource's callback is called. It can be
59
59
// called 0-N times for handles (e.g. TCPWrap), and will be called exactly 1
@@ -163,11 +163,11 @@ Key events in the lifetime of asynchronous events have been categorized into
163
163
four areas: instantiation, before/after the callback is called, and when the
164
164
instance is destructed.
165
165
166
- ##### ` init(asyncId, type, triggerId , resource) `
166
+ ##### ` init(asyncId, type, triggerAsyncId , resource) `
167
167
168
168
* ` asyncId ` {number} a unique ID for the async resource
169
169
* ` type ` {string} the type of the async resource
170
- * ` triggerId ` {number} the unique ID of the async resource in whose
170
+ * ` triggerAsyncId ` {number} the unique ID of the async resource in whose
171
171
execution context this async resource was created
172
172
* ` resource ` {Object} reference to the resource representing the async operation,
173
173
needs to be released during _ destroy_
@@ -214,20 +214,20 @@ when listening to the hooks.
214
214
215
215
###### ` triggerId `
216
216
217
- ` triggerId ` is the ` asyncId ` of the resource that caused (or "triggered") the
217
+ ` triggerAsyncId ` is the ` asyncId ` of the resource that caused (or "triggered") the
218
218
new resource to initialize and that caused ` init ` to call. This is different
219
- from ` async_hooks.currentId () ` that only shows * when* a resource was created,
220
- while ` triggerId ` shows * why* a resource was created.
219
+ from ` async_hooks.executionAsyncId () ` that only shows * when* a resource was
220
+ created, while ` triggerAsyncId ` shows * why* a resource was created.
221
221
222
222
223
- The following is a simple demonstration of ` triggerId ` :
223
+ The following is a simple demonstration of ` triggerAsyncId ` :
224
224
225
225
``` js
226
226
async_hooks .createHook ({
227
- init (asyncId , type , triggerId ) {
228
- const cId = async_hooks .currentId ();
227
+ init (asyncId , type , triggerAsyncId ) {
228
+ const eid = async_hooks .executionAsyncId ();
229
229
fs .writeSync (
230
- 1 , ` ${ type} (${ asyncId} ): trigger: ${ triggerId } scope : ${ cId } \n ` );
230
+ 1 , ` ${ type} (${ asyncId} ): trigger: ${ triggerAsyncId } execution : ${ eid } \n ` );
231
231
}
232
232
}).enable ();
233
233
@@ -237,18 +237,18 @@ require('net').createServer((conn) => {}).listen(8080);
237
237
Output when hitting the server with ` nc localhost 8080 ` :
238
238
239
239
```
240
- TCPWRAP(2): trigger: 1 scope : 1
241
- TCPWRAP(4): trigger: 2 scope : 0
240
+ TCPWRAP(2): trigger: 1 execution : 1
241
+ TCPWRAP(4): trigger: 2 execution : 0
242
242
```
243
243
244
244
The first ` TCPWRAP ` is the server which receives the connections.
245
245
246
246
The second ` TCPWRAP ` is the new connection from the client. When a new
247
247
connection is made the ` TCPWrap ` instance is immediately constructed. This
248
- happens outside of any JavaScript stack (side note: a ` currentId ()` of ` 0 `
248
+ happens outside of any JavaScript stack (side note: a ` executionAsyncId ()` of ` 0 `
249
249
means it's being executed from C++, with no JavaScript stack above it).
250
250
With only that information it would be impossible to link resources together in
251
- terms of what caused them to be created, so ` triggerId ` is given the task of
251
+ terms of what caused them to be created, so ` triggerAsyncId ` is given the task of
252
252
propagating what resource is responsible for the new resource's existence.
253
253
254
254
###### ` resource `
@@ -280,12 +280,13 @@ elaborate to make calling context easier to see.
280
280
``` js
281
281
let indent = 0 ;
282
282
async_hooks .createHook ({
283
- init (asyncId , type , triggerId ) {
284
- const cId = async_hooks .currentId ();
283
+ init (asyncId , type , triggerAsyncId ) {
284
+ const eid = async_hooks .executionAsyncId ();
285
285
const indentStr = ' ' .repeat (indent);
286
286
fs .writeSync (
287
287
1 ,
288
- ` ${ indentStr}${ type} (${ asyncId} ): trigger: ${ triggerId} scope: ${ cId} \n ` );
288
+ ` ${ indentStr}${ type} (${ asyncId} ):` +
289
+ ` trigger: ${ triggerAsyncId} execution: ${ eid} \n ` );
289
290
},
290
291
before (asyncId ) {
291
292
const indentStr = ' ' .repeat (indent);
@@ -306,28 +307,28 @@ async_hooks.createHook({
306
307
require (' net' ).createServer (() => {}).listen (8080 , () => {
307
308
// Let's wait 10ms before logging the server started.
308
309
setTimeout (() => {
309
- console .log (' >>>' , async_hooks .currentId ());
310
+ console .log (' >>>' , async_hooks .executionAsyncId ());
310
311
}, 10 );
311
312
});
312
313
```
313
314
314
315
Output from only starting the server:
315
316
316
317
```
317
- TCPWRAP(2): trigger: 1 scope : 1
318
- TickObject(3): trigger: 2 scope : 1
318
+ TCPWRAP(2): trigger: 1 execution : 1
319
+ TickObject(3): trigger: 2 execution : 1
319
320
before: 3
320
- Timeout(4): trigger: 3 scope : 3
321
- TIMERWRAP(5): trigger: 3 scope : 3
321
+ Timeout(4): trigger: 3 execution : 3
322
+ TIMERWRAP(5): trigger: 3 execution : 3
322
323
after: 3
323
324
destroy: 3
324
325
before: 5
325
326
before: 4
326
- TTYWRAP(6): trigger: 4 scope : 4
327
- SIGNALWRAP(7): trigger: 4 scope : 4
328
- TTYWRAP(8): trigger: 4 scope : 4
327
+ TTYWRAP(6): trigger: 4 execution : 4
328
+ SIGNALWRAP(7): trigger: 4 execution : 4
329
+ TTYWRAP(8): trigger: 4 execution : 4
329
330
>>> 4
330
- TickObject(9): trigger: 4 scope : 4
331
+ TickObject(9): trigger: 4 execution : 4
331
332
after: 4
332
333
after: 5
333
334
before: 9
@@ -337,11 +338,11 @@ destroy: 9
337
338
destroy: 5
338
339
```
339
340
340
- * Note* : As illustrated in the example, ` currentId ()` and ` scope ` each specify
341
- the value of the current execution context; which is delineated by calls to
342
- ` before ` and ` after ` .
341
+ * Note* : As illustrated in the example, ` executionAsyncId ()` and ` execution `
342
+ each specify the value of the current execution context; which is delineated by
343
+ calls to ` before ` and ` after ` .
343
344
344
- Only using ` scope ` to graph resource allocation results in the following:
345
+ Only using ` execution ` to graph resource allocation results in the following:
345
346
346
347
```
347
348
TTYWRAP(6) -> Timeout(4) -> TIMERWRAP(5) -> TickObject(3) -> root(1)
@@ -353,7 +354,7 @@ hostname is actually synchronous, but to maintain a completely asynchronous API
353
354
the user's callback is placed in a ` process.nextTick() ` .
354
355
355
356
The graph only shows * when* a resource was created, not * why* , so to track
356
- the * why* use ` triggerId ` .
357
+ the * why* use ` triggerAsyncId ` .
357
358
358
359
359
360
##### ` before(asyncId) `
@@ -396,39 +397,39 @@ the `resource` object passed to `init` it's possible that `destroy` is
396
397
never called, causing a memory leak in the application. Of course if
397
398
the resource doesn't depend on GC then this isn't an issue.
398
399
399
- #### ` async_hooks.currentId () `
400
+ #### ` async_hooks.executionAsyncId () `
400
401
401
402
* Returns {number} the ` asyncId ` of the current execution context. Useful to track
402
403
when something calls.
403
404
404
405
For example:
405
406
406
407
``` js
407
- console .log (async_hooks .currentId ()); // 1 - bootstrap
408
+ console .log (async_hooks .executionAsyncId ()); // 1 - bootstrap
408
409
fs .open (path, ' r' , (err , fd ) => {
409
- console .log (async_hooks .currentId ()); // 6 - open()
410
+ console .log (async_hooks .executionAsyncId ()); // 6 - open()
410
411
});
411
412
```
412
413
413
- It is important to note that the ID returned fom ` currentId ()` is related to
414
- execution timing, not causality (which is covered by ` triggerId ()` ). For
414
+ It is important to note that the ID returned fom ` executionAsyncId ()` is related
415
+ to execution timing, not causality (which is covered by ` triggerAsyncId ()` ). For
415
416
example:
416
417
417
418
``` js
418
419
const server = net .createServer (function onConnection (conn ) {
419
420
// Returns the ID of the server, not of the new connection, because the
420
421
// onConnection callback runs in the execution scope of the server's
421
422
// MakeCallback().
422
- async_hooks .currentId ();
423
+ async_hooks .executionAsyncId ();
423
424
424
425
}).listen (port, function onListening () {
425
426
// Returns the ID of a TickObject (i.e. process.nextTick()) because all
426
427
// callbacks passed to .listen() are wrapped in a nextTick().
427
- async_hooks .currentId ();
428
+ async_hooks .executionAsyncId ();
428
429
});
429
430
```
430
431
431
- #### ` async_hooks.triggerId () `
432
+ #### ` async_hooks.triggerAsyncId () `
432
433
433
434
* Returns {number} the ID of the resource responsible for calling the callback
434
435
that is currently being executed.
@@ -438,15 +439,15 @@ For example:
438
439
``` js
439
440
const server = net .createServer ((conn ) => {
440
441
// The resource that caused (or triggered) this callback to be called
441
- // was that of the new connection. Thus the return value of triggerId ()
442
+ // was that of the new connection. Thus the return value of triggerAsyncId ()
442
443
// is the asyncId of "conn".
443
- async_hooks .triggerId ();
444
+ async_hooks .triggerAsyncId ();
444
445
445
446
}).listen (port, () => {
446
447
// Even though all callbacks passed to .listen() are wrapped in a nextTick()
447
448
// the callback itself exists because the call to the server's .listen()
448
449
// was made. So the return value would be the ID of the server.
449
- async_hooks .triggerId ();
450
+ async_hooks .triggerAsyncId ();
450
451
});
451
452
```
452
453
@@ -475,9 +476,9 @@ The following is an overview of the `AsyncResource` API.
475
476
const { AsyncResource } = require (' async_hooks' );
476
477
477
478
// AsyncResource() is meant to be extended. Instantiating a
478
- // new AsyncResource() also triggers init. If triggerId is omitted then
479
- // async_hook.currentId () is used.
480
- const asyncResource = new AsyncResource (type, triggerId );
479
+ // new AsyncResource() also triggers init. If triggerAsyncId is omitted then
480
+ // async_hook.executionAsyncId () is used.
481
+ const asyncResource = new AsyncResource (type, triggerAsyncId );
481
482
482
483
// Call AsyncHooks before callbacks.
483
484
asyncResource .emitBefore ();
@@ -492,14 +493,14 @@ asyncResource.emitDestroy();
492
493
asyncResource .asyncId ();
493
494
494
495
// Return the trigger ID for the AsyncResource instance.
495
- asyncResource .triggerId ();
496
+ asyncResource .triggerAsyncId ();
496
497
```
497
498
498
- #### ` AsyncResource(type[, triggerId ]) `
499
+ #### ` AsyncResource(type[, triggerAsyncId ]) `
499
500
500
501
* arguments
501
502
* ` type ` {string} the type of ascyc event
502
- * ` triggerId ` {number} the ID of the execution context that created this async
503
+ * ` triggerAsyncId ` {number} the ID of the execution context that created this async
503
504
event
504
505
505
506
Example usage:
@@ -558,9 +559,9 @@ never be called.
558
559
559
560
* Returns {number} the unique ` asyncId ` assigned to the resource.
560
561
561
- #### ` asyncResource.triggerId () `
562
+ #### ` asyncResource.triggerAsyncId () `
562
563
563
- * Returns {number} the same ` triggerId ` that is passed to the ` AsyncResource `
564
+ * Returns {number} the same ` triggerAsyncId ` that is passed to the ` AsyncResource `
564
565
constructor.
565
566
566
567
[ `Hook Callbacks` ] : #hook-callbacks
0 commit comments