Skip to content

Commit

Permalink
doc: replace 1 by process.stdout.fd
Browse files Browse the repository at this point in the history
PR-URL: nodejs#22564
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
starkwang committed Aug 29, 2018
1 parent 6e942e7 commit 2584052
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions doc/api/async_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ Because printing to the console is an asynchronous operation, `console.log()`
will cause the AsyncHooks callbacks to be called. Using `console.log()` or
similar asynchronous operations inside an AsyncHooks callback function will thus
cause an infinite recursion. An easy solution to this when debugging is to use a
synchronous logging operation such as `fs.writeSync(1, msg)`. This will print to
stdout because `1` is the file descriptor for stdout and will not invoke
AsyncHooks recursively because it is synchronous.
synchronous logging operation such as `fs.writeSync(process.stdout.fd, msg)`.
This will print to stdout and will not invoke AsyncHooks recursively because it
is synchronous.

```js
const fs = require('fs');
const util = require('util');

function debug(...args) {
// use a function like this one when debugging inside an AsyncHooks callback
fs.writeSync(1, `${util.format(...args)}\n`);
fs.writeSync(process.stdout.fd, `${util.format(...args)}\n`);
}
```

Expand Down Expand Up @@ -330,17 +330,17 @@ async_hooks.createHook({
},
before(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}before: ${asyncId}\n`);
fs.writeSync(process.stdout.fd, `${indentStr}before: ${asyncId}\n`);
indent += 2;
},
after(asyncId) {
indent -= 2;
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}after: ${asyncId}\n`);
fs.writeSync(process.stdout.fd, `${indentStr}after: ${asyncId}\n`);
},
destroy(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}destroy: ${asyncId}\n`);
fs.writeSync(process.stdout.fd, `${indentStr}destroy: ${asyncId}\n`);
},
}).enable();

Expand Down

0 comments on commit 2584052

Please sign in to comment.