@@ -220,13 +220,11 @@ while `triggerId` shows *why* a resource was created.
220220The following is a simple demonstration of ` triggerId ` :
221221
222222``` js
223- const async_hooks = require (' async_hooks' );
224-
225223async_hooks .createHook ({
226224 init (asyncId , type , triggerId ) {
227225 const cId = async_hooks .currentId ();
228- fs .writeSync (1 , ` ${ type } ( ${ asyncId } ): ` +
229- ` trigger: ${ triggerId} scope: ${ cId} \n ` );
226+ fs .writeSync (
227+ 1 , ` ${ type } ( ${ asyncId } ): trigger: ${ triggerId} scope: ${ cId} \n ` );
230228 }
231229}).enable ();
232230
@@ -271,25 +269,28 @@ callback to `listen()` will look like. The output formatting is slightly more
271269elaborate to make calling context easier to see.
272270
273271``` js
274- const async_hooks = require (' async_hooks' );
275-
276272let indent = 0 ;
277273async_hooks .createHook ({
278274 init (asyncId , type , triggerId ) {
279275 const cId = async_hooks .currentId ();
280- fs .writeSync (1 , ' ' .repeat (indent) + ` ${ type} (${ asyncId} ): ` +
281- ` trigger: ${ triggerId} scope: ${ cId} \n ` );
276+ const indentStr = ' ' .repeat (indent);
277+ fs .writeSync (
278+ 1 ,
279+ ` ${ indentStr}${ type} (${ asyncId} ): trigger: ${ triggerId} scope: ${ cId} \n ` );
282280 },
283281 before (asyncId ) {
284- fs .writeSync (1 , ' ' .repeat (indent) + ` before: ${ asyncId} ` );
282+ const indentStr = ' ' .repeat (indent);
283+ fs .writeSync (1 , ` ${ indentStr} before: ${ asyncId} \n ` );
285284 indent += 2 ;
286285 },
287286 after (asyncId ) {
288287 indent -= 2 ;
289- fs .writeSync (1 , ' ' .repeat (indent) + ` after: ${ asyncId} ` );
288+ const indentStr = ' ' .repeat (indent);
289+ fs .writeSync (1 , ` ${ indentStr} after: ${ asyncId} \n ` );
290290 },
291291 destroy (asyncId ) {
292- fs .writeSync (1 , ' ' .repeat (indent) + ` destroy: ${ asyncId} ` );
292+ const indentStr = ' ' .repeat (indent);
293+ fs .writeSync (1 , ` ${ indentStr} destroy: ${ asyncId} \n ` );
293294 },
294295}).enable ();
295296
@@ -319,10 +320,10 @@ before: 5
319320>>> 4
320321 TickObject(9): trigger: 4 scope: 4
321322 after: 4
322- destroy: 4
323323after: 5
324324before: 9
325325after: 9
326+ destroy: 4
326327destroy: 9
327328destroy: 5
328329```
@@ -395,8 +396,8 @@ For example:
395396
396397``` js
397398console .log (async_hooks .currentId ()); // 1 - bootstrap
398- fs .open (path, (err , fd ) => {
399- console .log (async_hooks .currentId ()); // 2 - open()
399+ fs .open (path, ' r ' , (err , fd ) => {
400+ console .log (async_hooks .currentId ()); // 6 - open()
400401});
401402```
402403
@@ -427,9 +428,9 @@ For example:
427428
428429``` js
429430const server = net .createServer ((conn ) => {
430- // Though the resource that caused (or triggered) this callback to
431- // be called was that of the new connection. Thus the return value
432- // of triggerId() is the ID of "conn".
431+ // The resource that caused (or triggered) this callback to be called
432+ // was that of the new connection. Thus the return value of triggerId()
433+ // is the asyncId of "conn".
433434 async_hooks .triggerId ();
434435
435436}).listen (port, () => {
@@ -462,6 +463,8 @@ will occur and node will abort.
462463The following is an overview of the ` AsyncResource ` API.
463464
464465``` js
466+ const { AsyncResource } = require (' async_hooks' );
467+
465468// AsyncResource() is meant to be extended. Instantiating a
466469// new AsyncResource() also triggers init. If triggerId is omitted then
467470// async_hook.currentId() is used.
0 commit comments