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

async_hooks,doc: cache process.stdout.fd outside init #38382

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions doc/api/async_hooks.md
Expand Up @@ -271,11 +271,16 @@ created, while `triggerAsyncId` shows *why* a resource was created.
The following is a simple demonstration of `triggerAsyncId`:

```js
const async_hooks = require('async_hooks');
const fs = require('fs');
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved

const { fd } = process.stdout;

async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
const eid = async_hooks.executionAsyncId();
fs.writeSync(
process.stdout.fd,
fd,
`${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`);
}
}).enable();
Expand Down Expand Up @@ -322,29 +327,34 @@ callback to `listen()` will look like. The output formatting is slightly more
elaborate to make calling context easier to see.

```js
const async_hooks = require('async_hooks');
const fs = require('fs');

const { fd } = process.stdout;

let indent = 0;
async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
const eid = async_hooks.executionAsyncId();
const indentStr = ' '.repeat(indent);
fs.writeSync(
process.stdout.fd,
fd,
`${indentStr}${type}(${asyncId}):` +
` trigger: ${triggerAsyncId} execution: ${eid}\n`);
},
before(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(process.stdout.fd, `${indentStr}before: ${asyncId}\n`);
fs.writeSync(fd, `${indentStr}before: ${asyncId}\n`);
indent += 2;
},
after(asyncId) {
indent -= 2;
const indentStr = ' '.repeat(indent);
fs.writeSync(process.stdout.fd, `${indentStr}after: ${asyncId}\n`);
fs.writeSync(fd, `${indentStr}after: ${asyncId}\n`);
},
destroy(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(process.stdout.fd, `${indentStr}destroy: ${asyncId}\n`);
fs.writeSync(fd, `${indentStr}destroy: ${asyncId}\n`);
},
}).enable();

Expand Down