镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
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
18 changes: 11 additions & 7 deletions doc/api/async_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,18 @@ created, while `triggerAsyncId` shows *why* a resource was created.
The following is a simple demonstration of `triggerAsyncId`:

```js
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();

require('net').createServer((conn) => {}).listen(8080);
net.createServer((conn) => {}).listen(8080);
```

Output when hitting the server with `nc localhost 8080`:
Expand Down Expand Up @@ -322,33 +324,35 @@ callback to `listen()` will look like. The output formatting is slightly more
elaborate to make calling context easier to see.

```js
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();

require('net').createServer(() => {}).listen(8080, () => {
net.createServer(() => {}).listen(8080, () => {
// Let's wait 10ms before logging the server started.
setTimeout(() => {
console.log('>>>', async_hooks.executionAsyncId());
Expand Down