@@ -271,16 +271,18 @@ created, while `triggerAsyncId` shows *why* a resource was created.
271
271
The following is a simple demonstration of ` triggerAsyncId ` :
272
272
273
273
``` js
274
+ const { fd } = process .stdout ;
275
+
274
276
async_hooks .createHook ({
275
277
init (asyncId , type , triggerAsyncId ) {
276
278
const eid = async_hooks .executionAsyncId ();
277
279
fs .writeSync (
278
- process . stdout . fd ,
280
+ fd,
279
281
` ${ type} (${ asyncId} ): trigger: ${ triggerAsyncId} execution: ${ eid} \n ` );
280
282
}
281
283
}).enable ();
282
284
283
- require ( ' net' ) .createServer ((conn ) => {}).listen (8080 );
285
+ net .createServer ((conn ) => {}).listen (8080 );
284
286
```
285
287
286
288
Output when hitting the server with ` nc localhost 8080 ` :
@@ -322,33 +324,35 @@ callback to `listen()` will look like. The output formatting is slightly more
322
324
elaborate to make calling context easier to see.
323
325
324
326
``` js
327
+ const { fd } = process .stdout ;
328
+
325
329
let indent = 0 ;
326
330
async_hooks .createHook ({
327
331
init (asyncId , type , triggerAsyncId ) {
328
332
const eid = async_hooks .executionAsyncId ();
329
333
const indentStr = ' ' .repeat (indent);
330
334
fs .writeSync (
331
- process . stdout . fd ,
335
+ fd,
332
336
` ${ indentStr}${ type} (${ asyncId} ):` +
333
337
` trigger: ${ triggerAsyncId} execution: ${ eid} \n ` );
334
338
},
335
339
before (asyncId ) {
336
340
const indentStr = ' ' .repeat (indent);
337
- fs .writeSync (process . stdout . fd , ` ${ indentStr} before: ${ asyncId} \n ` );
341
+ fs .writeSync (fd, ` ${ indentStr} before: ${ asyncId} \n ` );
338
342
indent += 2 ;
339
343
},
340
344
after (asyncId ) {
341
345
indent -= 2 ;
342
346
const indentStr = ' ' .repeat (indent);
343
- fs .writeSync (process . stdout . fd , ` ${ indentStr} after: ${ asyncId} \n ` );
347
+ fs .writeSync (fd, ` ${ indentStr} after: ${ asyncId} \n ` );
344
348
},
345
349
destroy (asyncId ) {
346
350
const indentStr = ' ' .repeat (indent);
347
- fs .writeSync (process . stdout . fd , ` ${ indentStr} destroy: ${ asyncId} \n ` );
351
+ fs .writeSync (fd, ` ${ indentStr} destroy: ${ asyncId} \n ` );
348
352
},
349
353
}).enable ();
350
354
351
- require ( ' net' ) .createServer (() => {}).listen (8080 , () => {
355
+ net .createServer (() => {}).listen (8080 , () => {
352
356
// Let's wait 10ms before logging the server started.
353
357
setTimeout (() => {
354
358
console .log (' >>>' , async_hooks .executionAsyncId ());
0 commit comments