@@ -150,17 +150,17 @@ Because printing to the console is an asynchronous operation, `console.log()`
150150will cause the AsyncHooks callbacks to be called. Using ` console.log() ` or
151151similar asynchronous operations inside an AsyncHooks callback function will thus
152152cause an infinite recursion. An easy solution to this when debugging is to use a
153- synchronous logging operation such as ` fs.writeSync(1 , msg) ` . This will print to
154- stdout because ` 1 ` is the file descriptor for stdout and will not invoke
155- AsyncHooks recursively because it is synchronous.
153+ synchronous logging operation such as ` fs.writeSync(process.stdout.fd , msg) ` .
154+ This will print to stdout and will not invoke AsyncHooks recursively because it
155+ is synchronous.
156156
157157``` js
158158const fs = require (' fs' );
159159const util = require (' util' );
160160
161161function debug (... args ) {
162162 // use a function like this one when debugging inside an AsyncHooks callback
163- fs .writeSync (1 , ` ${ util .format (... args)} \n ` );
163+ fs .writeSync (process . stdout . fd , ` ${ util .format (... args)} \n ` );
164164}
165165```
166166
@@ -330,17 +330,17 @@ async_hooks.createHook({
330330 },
331331 before (asyncId ) {
332332 const indentStr = ' ' .repeat (indent);
333- fs .writeSync (1 , ` ${ indentStr} before: ${ asyncId} \n ` );
333+ fs .writeSync (process . stdout . fd , ` ${ indentStr} before: ${ asyncId} \n ` );
334334 indent += 2 ;
335335 },
336336 after (asyncId ) {
337337 indent -= 2 ;
338338 const indentStr = ' ' .repeat (indent);
339- fs .writeSync (1 , ` ${ indentStr} after: ${ asyncId} \n ` );
339+ fs .writeSync (process . stdout . fd , ` ${ indentStr} after: ${ asyncId} \n ` );
340340 },
341341 destroy (asyncId ) {
342342 const indentStr = ' ' .repeat (indent);
343- fs .writeSync (1 , ` ${ indentStr} destroy: ${ asyncId} \n ` );
343+ fs .writeSync (process . stdout . fd , ` ${ indentStr} destroy: ${ asyncId} \n ` );
344344 },
345345}).enable ();
346346
0 commit comments