Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: hide undocumented object artifacts in async_hooks examples #24741

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 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(process.stdout.fd, msg)`.
This will print to stdout and will not invoke AsyncHooks recursively because it
is synchronous.
synchronous logging operation such as `fs.writeFileSync(file, msg, flag)`.
This will print to the file 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(process.stdout.fd, `${util.format(...args)}\n`);
fs.writeFileSync('log.out', `${util.format(...args)}\n`, { flag: 'a' });
}
```

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

Expand Down