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: #22564
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
starkwang authored and targos committed Sep 3, 2018
1 parent 2260bb9 commit 1d38399
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 1d38399

Please sign in to comment.