summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/api/async_hooks.md43
1 files changed, 42 insertions, 1 deletions
diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md
index 59b2284f29..1b408ba97b 100644
--- a/doc/api/async_hooks.md
+++ b/doc/api/async_hooks.md
@@ -445,7 +445,48 @@ The following is an example with additional information about the calls to
callback to `listen()` will look like. The output formatting is slightly more
elaborate to make calling context easier to see.
-```js
+```mjs
+import async_hooks from 'node:async_hooks';
+import fs from 'node:fs';
+import net from 'node:net';
+import { stdout } from 'node:process';
+const { fd } = stdout;
+
+let indent = 0;
+async_hooks.createHook({
+ init(asyncId, type, triggerAsyncId) {
+ const eid = async_hooks.executionAsyncId();
+ const indentStr = ' '.repeat(indent);
+ fs.writeSync(
+ fd,
+ `${indentStr}${type}(${asyncId}):` +
+ ` trigger: ${triggerAsyncId} execution: ${eid}\n`);
+ },
+ before(asyncId) {
+ const indentStr = ' '.repeat(indent);
+ fs.writeSync(fd, `${indentStr}before: ${asyncId}\n`);
+ indent += 2;
+ },
+ after(asyncId) {
+ indent -= 2;
+ const indentStr = ' '.repeat(indent);
+ fs.writeSync(fd, `${indentStr}after: ${asyncId}\n`);
+ },
+ destroy(asyncId) {
+ const indentStr = ' '.repeat(indent);
+ fs.writeSync(fd, `${indentStr}destroy: ${asyncId}\n`);
+ },
+}).enable();
+
+net.createServer(() => {}).listen(8080, () => {
+ // Let's wait 10ms before logging the server started.
+ setTimeout(() => {
+ console.log('>>>', async_hooks.executionAsyncId());
+ }, 10);
+});
+```
+
+```cjs
const async_hooks = require('node:async_hooks');
const fs = require('node:fs');
const net = require('node:net');