Skip to content

Commit 997c0e0

Browse files
gireeshpunathilBridgeAR
authored andcommitted
doc: hide undocumented object artifacts in async_hooks
The examples show `process.stdout.fd` as a means to use synchronous writes in async_hooks context. However this is an undocumented field, so showcase a file write example instead. Fixes: #22873 PR-URL: #24741 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
1 parent 58e5c00 commit 997c0e0

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

doc/api/async_hooks.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@ Because printing to the console is an asynchronous operation, `console.log()`
150150
will cause the AsyncHooks callbacks to be called. Using `console.log()` or
151151
similar asynchronous operations inside an AsyncHooks callback function will thus
152152
cause an infinite recursion. An easy solution to this when debugging is to use a
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.
153+
synchronous logging operation such as `fs.writeFileSync(file, msg, flag)`.
154+
This will print to the file and will not invoke AsyncHooks recursively because
155+
it is synchronous.
156156

157157
```js
158158
const fs = require('fs');
159159
const util = require('util');
160160

161161
function debug(...args) {
162162
// use a function like this one when debugging inside an AsyncHooks callback
163-
fs.writeSync(process.stdout.fd, `${util.format(...args)}\n`);
163+
fs.writeFileSync('log.out', `${util.format(...args)}\n`, { flag: 'a' });
164164
}
165165
```
166166

@@ -330,17 +330,20 @@ async_hooks.createHook({
330330
},
331331
before(asyncId) {
332332
const indentStr = ' '.repeat(indent);
333-
fs.writeSync(process.stdout.fd, `${indentStr}before: ${asyncId}\n`);
333+
fs.writeFileSync('log.out',
334+
`${indentStr}before: ${asyncId}\n`, { flag: 'a' });
334335
indent += 2;
335336
},
336337
after(asyncId) {
337338
indent -= 2;
338339
const indentStr = ' '.repeat(indent);
339-
fs.writeSync(process.stdout.fd, `${indentStr}after: ${asyncId}\n`);
340+
fs.writeFileSync('log.out',
341+
`${indentStr}after: ${asyncId}\n`, { flag: 'a' });
340342
},
341343
destroy(asyncId) {
342344
const indentStr = ' '.repeat(indent);
343-
fs.writeSync(process.stdout.fd, `${indentStr}destroy: ${asyncId}\n`);
345+
fs.writeFileSync('log.out',
346+
`${indentStr}destroy: ${asyncId}\n`, { flag: 'a' });
344347
},
345348
}).enable();
346349

0 commit comments

Comments
 (0)