@@ -220,13 +220,11 @@ while `triggerId` shows *why* a resource was created.
220
220
The following is a simple demonstration of ` triggerId ` :
221
221
222
222
``` js
223
- const async_hooks = require (' async_hooks' );
224
-
225
223
async_hooks .createHook ({
226
224
init (asyncId , type , triggerId ) {
227
225
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 ` );
230
228
}
231
229
}).enable ();
232
230
@@ -271,25 +269,28 @@ callback to `listen()` will look like. The output formatting is slightly more
271
269
elaborate to make calling context easier to see.
272
270
273
271
``` js
274
- const async_hooks = require (' async_hooks' );
275
-
276
272
let indent = 0 ;
277
273
async_hooks .createHook ({
278
274
init (asyncId , type , triggerId ) {
279
275
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 ` );
282
280
},
283
281
before (asyncId ) {
284
- fs .writeSync (1 , ' ' .repeat (indent) + ` before: ${ asyncId} ` );
282
+ const indentStr = ' ' .repeat (indent);
283
+ fs .writeSync (1 , ` ${ indentStr} before: ${ asyncId} \n ` );
285
284
indent += 2 ;
286
285
},
287
286
after (asyncId ) {
288
287
indent -= 2 ;
289
- fs .writeSync (1 , ' ' .repeat (indent) + ` after: ${ asyncId} ` );
288
+ const indentStr = ' ' .repeat (indent);
289
+ fs .writeSync (1 , ` ${ indentStr} after: ${ asyncId} \n ` );
290
290
},
291
291
destroy (asyncId ) {
292
- fs .writeSync (1 , ' ' .repeat (indent) + ` destroy: ${ asyncId} ` );
292
+ const indentStr = ' ' .repeat (indent);
293
+ fs .writeSync (1 , ` ${ indentStr} destroy: ${ asyncId} \n ` );
293
294
},
294
295
}).enable ();
295
296
@@ -319,10 +320,10 @@ before: 5
319
320
>>> 4
320
321
TickObject(9): trigger: 4 scope: 4
321
322
after: 4
322
- destroy: 4
323
323
after: 5
324
324
before: 9
325
325
after: 9
326
+ destroy: 4
326
327
destroy: 9
327
328
destroy: 5
328
329
```
@@ -395,8 +396,8 @@ For example:
395
396
396
397
``` js
397
398
console .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()
400
401
});
401
402
```
402
403
@@ -427,9 +428,9 @@ For example:
427
428
428
429
``` js
429
430
const 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".
433
434
async_hooks .triggerId ();
434
435
435
436
}).listen (port, () => {
@@ -462,6 +463,8 @@ will occur and node will abort.
462
463
The following is an overview of the ` AsyncResource ` API.
463
464
464
465
``` js
466
+ const { AsyncResource } = require (' async_hooks' );
467
+
465
468
// AsyncResource() is meant to be extended. Instantiating a
466
469
// new AsyncResource() also triggers init. If triggerId is omitted then
467
470
// async_hook.currentId() is used.
0 commit comments