@@ -29,11 +29,11 @@ Following is a simple overview of the public API.
2929const async_hooks = require (' async_hooks' );
3030
3131// Return the ID of the current execution context.
32- const cid = async_hooks .currentId ();
32+ const eid = async_hooks .executionAsyncId ();
3333
3434// Return the ID of the handle responsible for triggering the callback of the
3535// current execution scope to call.
36- const tid = async_hooks .triggerId ();
36+ const tid = async_hooks .triggerAsyncId ();
3737
3838// Create a new AsyncHook instance. All of these callbacks are optional.
3939const asyncHook = async_hooks .createHook ({ init, before, after, destroy });
@@ -53,7 +53,7 @@ asyncHook.disable();
5353// init is called during object construction. The resource may not have
5454// completed construction when this callback runs, therefore all fields of the
5555// resource referenced by "asyncId" may not have been populated.
56- function init (asyncId , type , triggerId , resource ) { }
56+ function init (asyncId , type , triggerAsyncId , resource ) { }
5757
5858// before is called just before the resource's callback is called. It can be
5959// 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
163163four areas: instantiation, before/after the callback is called, and when the
164164instance is destructed.
165165
166- ##### ` init(asyncId, type, triggerId , resource) `
166+ ##### ` init(asyncId, type, triggerAsyncId , resource) `
167167
168168* ` asyncId ` {number} a unique ID for the async resource
169169* ` 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
171171 execution context this async resource was created
172172* ` resource ` {Object} reference to the resource representing the async operation,
173173 needs to be released during _ destroy_
@@ -214,20 +214,20 @@ when listening to the hooks.
214214
215215###### ` triggerId `
216216
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
218218new 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.
221221
222222
223- The following is a simple demonstration of ` triggerId ` :
223+ The following is a simple demonstration of ` triggerAsyncId ` :
224224
225225``` js
226226async_hooks .createHook ({
227- init (asyncId , type , triggerId ) {
228- const cId = async_hooks .currentId ();
227+ init (asyncId , type , triggerAsyncId ) {
228+ const eid = async_hooks .executionAsyncId ();
229229 fs .writeSync (
230- 1 , ` ${ type} (${ asyncId} ): trigger: ${ triggerId } scope : ${ cId } \n ` );
230+ 1 , ` ${ type} (${ asyncId} ): trigger: ${ triggerAsyncId } execution : ${ eid } \n ` );
231231 }
232232}).enable ();
233233
@@ -237,18 +237,18 @@ require('net').createServer((conn) => {}).listen(8080);
237237Output when hitting the server with ` nc localhost 8080 ` :
238238
239239```
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
242242```
243243
244244The first ` TCPWRAP ` is the server which receives the connections.
245245
246246The second ` TCPWRAP ` is the new connection from the client. When a new
247247connection 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 `
249249means it's being executed from C++, with no JavaScript stack above it).
250250With 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
252252propagating what resource is responsible for the new resource's existence.
253253
254254###### ` resource `
@@ -280,12 +280,13 @@ elaborate to make calling context easier to see.
280280``` js
281281let indent = 0 ;
282282async_hooks .createHook ({
283- init (asyncId , type , triggerId ) {
284- const cId = async_hooks .currentId ();
283+ init (asyncId , type , triggerAsyncId ) {
284+ const eid = async_hooks .executionAsyncId ();
285285 const indentStr = ' ' .repeat (indent);
286286 fs .writeSync (
287287 1 ,
288- ` ${ indentStr}${ type} (${ asyncId} ): trigger: ${ triggerId} scope: ${ cId} \n ` );
288+ ` ${ indentStr}${ type} (${ asyncId} ):` +
289+ ` trigger: ${ triggerAsyncId} execution: ${ eid} \n ` );
289290 },
290291 before (asyncId ) {
291292 const indentStr = ' ' .repeat (indent);
@@ -306,28 +307,28 @@ async_hooks.createHook({
306307require (' net' ).createServer (() => {}).listen (8080 , () => {
307308 // Let's wait 10ms before logging the server started.
308309 setTimeout (() => {
309- console .log (' >>>' , async_hooks .currentId ());
310+ console .log (' >>>' , async_hooks .executionAsyncId ());
310311 }, 10 );
311312});
312313```
313314
314315Output from only starting the server:
315316
316317```
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
319320before: 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
322323after: 3
323324destroy: 3
324325before: 5
325326 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
329330>>> 4
330- TickObject(9): trigger: 4 scope : 4
331+ TickObject(9): trigger: 4 execution : 4
331332 after: 4
332333after: 5
333334before: 9
@@ -337,11 +338,11 @@ destroy: 9
337338destroy: 5
338339```
339340
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 ` .
343344
344- Only using ` scope ` to graph resource allocation results in the following:
345+ Only using ` execution ` to graph resource allocation results in the following:
345346
346347```
347348TTYWRAP(6) -> Timeout(4) -> TIMERWRAP(5) -> TickObject(3) -> root(1)
@@ -353,7 +354,7 @@ hostname is actually synchronous, but to maintain a completely asynchronous API
353354the user's callback is placed in a ` process.nextTick() ` .
354355
355356The graph only shows * when* a resource was created, not * why* , so to track
356- the * why* use ` triggerId ` .
357+ the * why* use ` triggerAsyncId ` .
357358
358359
359360##### ` before(asyncId) `
@@ -396,39 +397,39 @@ the `resource` object passed to `init` it's possible that `destroy` is
396397never called, causing a memory leak in the application. Of course if
397398the resource doesn't depend on GC then this isn't an issue.
398399
399- #### ` async_hooks.currentId () `
400+ #### ` async_hooks.executionAsyncId () `
400401
401402* Returns {number} the ` asyncId ` of the current execution context. Useful to track
402403 when something calls.
403404
404405For example:
405406
406407``` js
407- console .log (async_hooks .currentId ()); // 1 - bootstrap
408+ console .log (async_hooks .executionAsyncId ()); // 1 - bootstrap
408409fs .open (path, ' r' , (err , fd ) => {
409- console .log (async_hooks .currentId ()); // 6 - open()
410+ console .log (async_hooks .executionAsyncId ()); // 6 - open()
410411});
411412```
412413
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
415416example:
416417
417418``` js
418419const server = net .createServer (function onConnection (conn ) {
419420 // Returns the ID of the server, not of the new connection, because the
420421 // onConnection callback runs in the execution scope of the server's
421422 // MakeCallback().
422- async_hooks .currentId ();
423+ async_hooks .executionAsyncId ();
423424
424425}).listen (port, function onListening () {
425426 // Returns the ID of a TickObject (i.e. process.nextTick()) because all
426427 // callbacks passed to .listen() are wrapped in a nextTick().
427- async_hooks .currentId ();
428+ async_hooks .executionAsyncId ();
428429});
429430```
430431
431- #### ` async_hooks.triggerId () `
432+ #### ` async_hooks.triggerAsyncId () `
432433
433434* Returns {number} the ID of the resource responsible for calling the callback
434435 that is currently being executed.
@@ -438,15 +439,15 @@ For example:
438439``` js
439440const server = net .createServer ((conn ) => {
440441 // 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 ()
442443 // is the asyncId of "conn".
443- async_hooks .triggerId ();
444+ async_hooks .triggerAsyncId ();
444445
445446}).listen (port, () => {
446447 // Even though all callbacks passed to .listen() are wrapped in a nextTick()
447448 // the callback itself exists because the call to the server's .listen()
448449 // was made. So the return value would be the ID of the server.
449- async_hooks .triggerId ();
450+ async_hooks .triggerAsyncId ();
450451});
451452```
452453
@@ -475,9 +476,9 @@ The following is an overview of the `AsyncResource` API.
475476const { AsyncResource } = require (' async_hooks' );
476477
477478// 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 );
481482
482483// Call AsyncHooks before callbacks.
483484asyncResource .emitBefore ();
@@ -492,14 +493,14 @@ asyncResource.emitDestroy();
492493asyncResource .asyncId ();
493494
494495// Return the trigger ID for the AsyncResource instance.
495- asyncResource .triggerId ();
496+ asyncResource .triggerAsyncId ();
496497```
497498
498- #### ` AsyncResource(type[, triggerId ]) `
499+ #### ` AsyncResource(type[, triggerAsyncId ]) `
499500
500501* arguments
501502 * ` 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
503504 event
504505
505506Example usage:
@@ -558,9 +559,9 @@ never be called.
558559
559560* Returns {number} the unique ` asyncId ` assigned to the resource.
560561
561- #### ` asyncResource.triggerId () `
562+ #### ` asyncResource.triggerAsyncId () `
562563
563- * Returns {number} the same ` triggerId ` that is passed to the ` AsyncResource `
564+ * Returns {number} the same ` triggerAsyncId ` that is passed to the ` AsyncResource `
564565constructor.
565566
566567[ `Hook Callbacks` ] : #hook-callbacks
0 commit comments