Skip to content

Commit ad26bb5

Browse files
gireeshpunathilrefack
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: nodejs#22873 PR-URL: nodejs#24741 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]>
1 parent 0f924a6 commit ad26bb5

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

@@ -329,17 +329,20 @@ async_hooks.createHook({
329329
},
330330
before(asyncId) {
331331
const indentStr = ' '.repeat(indent);
332-
fs.writeSync(process.stdout.fd, `${indentStr}before: ${asyncId}\n`);
332+
fs.writeFileSync('log.out',
333+
`${indentStr}before: ${asyncId}\n`, { flag: 'a' });
333334
indent += 2;
334335
},
335336
after(asyncId) {
336337
indent -= 2;
337338
const indentStr = ' '.repeat(indent);
338-
fs.writeSync(process.stdout.fd, `${indentStr}after: ${asyncId}\n`);
339+
fs.writeFileSync('log.out',
340+
`${indentStr}after: ${asyncId}\n`, { flag: 'a' });
339341
},
340342
destroy(asyncId) {
341343
const indentStr = ' '.repeat(indent);
342-
fs.writeSync(process.stdout.fd, `${indentStr}destroy: ${asyncId}\n`);
344+
fs.writeFileSync('log.out',
345+
`${indentStr}destroy: ${asyncId}\n`, { flag: 'a' });
343346
},
344347
}).enable();
345348

0 commit comments

Comments
 (0)