Skip to content

Commit 46d3d23

Browse files
Trottbengl
authored andcommitted
doc: revise async_hooks docs
The only significant change is to replace AsyncHooks with `AsyncHook`. PR-URL: #42337 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Mestery <[email protected]>
1 parent 5367002 commit 46d3d23

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Diff for: doc/api/async_hooks.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const async_hooks = require('async_hooks');
2020
## Terminology
2121

2222
An asynchronous resource represents an object with an associated callback.
23-
This callback may be called multiple times, for example, the `'connection'`
23+
This callback may be called multiple times, such as the `'connection'`
2424
event in `net.createServer()`, or just a single time like in `fs.open()`.
2525
A resource can also be closed before the callback is called. `AsyncHook` does
2626
not explicitly distinguish between these different cases but will represent them
@@ -209,22 +209,22 @@ future. This is subject to change in the future if a comprehensive analysis is
209209
performed to ensure an exception can follow the normal control flow without
210210
unintentional side effects.
211211

212-
### Printing in AsyncHooks callbacks
212+
### Printing in `AsyncHook` callbacks
213213

214214
Because printing to the console is an asynchronous operation, `console.log()`
215-
will cause the AsyncHooks callbacks to be called. Using `console.log()` or
216-
similar asynchronous operations inside an AsyncHooks callback function will thus
215+
will cause `AsyncHook` callbacks to be called. Using `console.log()` or
216+
similar asynchronous operations inside an `AsyncHook` callback function will
217217
cause an infinite recursion. An easy solution to this when debugging is to use a
218218
synchronous logging operation such as `fs.writeFileSync(file, msg, flag)`.
219-
This will print to the file and will not invoke AsyncHooks recursively because
219+
This will print to the file and will not invoke `AsyncHook` recursively because
220220
it is synchronous.
221221

222222
```mjs
223223
import { writeFileSync } from 'fs';
224224
import { format } from 'util';
225225

226226
function debug(...args) {
227-
// Use a function like this one when debugging inside an AsyncHooks callback
227+
// Use a function like this one when debugging inside an AsyncHook callback
228228
writeFileSync('log.out', `${format(...args)}\n`, { flag: 'a' });
229229
}
230230
```
@@ -234,16 +234,16 @@ const fs = require('fs');
234234
const util = require('util');
235235

236236
function debug(...args) {
237-
// Use a function like this one when debugging inside an AsyncHooks callback
237+
// Use a function like this one when debugging inside an AsyncHook callback
238238
fs.writeFileSync('log.out', `${util.format(...args)}\n`, { flag: 'a' });
239239
}
240240
```
241241

242242
If an asynchronous operation is needed for logging, it is possible to keep
243243
track of what caused the asynchronous operation using the information
244-
provided by AsyncHooks itself. The logging should then be skipped when
245-
it was the logging itself that caused AsyncHooks callback to call. By
246-
doing this the otherwise infinite recursion is broken.
244+
provided by `AsyncHook` itself. The logging should then be skipped when
245+
it was the logging itself that caused the `AsyncHook` callback to be called. By
246+
doing this, the otherwise infinite recursion is broken.
247247

248248
## Class: `AsyncHook`
249249

0 commit comments

Comments
 (0)