doc: replace 1 by process.stdout.fd

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Weijia Wang 2018-08-28 15:15:00 +08:00
parent 6e942e7353
commit 25840529e3

View File

@ -150,9 +150,9 @@ 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');
@ -160,7 +160,7 @@ 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`);
}
```
@ -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();