doc: fix nits in code examples of async_hooks.md
* Make `require()` consistent. * Add missing argument. * Add missing `\n` in outputs. * Reduce string concatenations. * Update outputs. * Reword and fix a typo. PR-URL: https://github.com/nodejs/node/pull/13400 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
parent
ce8bce497c
commit
5d9dc94509
@ -220,13 +220,11 @@ while `triggerId` shows *why* a resource was created.
|
|||||||
The following is a simple demonstration of `triggerId`:
|
The following is a simple demonstration of `triggerId`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const async_hooks = require('async_hooks');
|
|
||||||
|
|
||||||
async_hooks.createHook({
|
async_hooks.createHook({
|
||||||
init(asyncId, type, triggerId) {
|
init(asyncId, type, triggerId) {
|
||||||
const cId = async_hooks.currentId();
|
const cId = async_hooks.currentId();
|
||||||
fs.writeSync(1, `${type}(${asyncId}): ` +
|
fs.writeSync(
|
||||||
`trigger: ${triggerId} scope: ${cId}\n`);
|
1, `${type}(${asyncId}): trigger: ${triggerId} scope: ${cId}\n`);
|
||||||
}
|
}
|
||||||
}).enable();
|
}).enable();
|
||||||
|
|
||||||
@ -271,25 +269,28 @@ callback to `listen()` will look like. The output formatting is slightly more
|
|||||||
elaborate to make calling context easier to see.
|
elaborate to make calling context easier to see.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const async_hooks = require('async_hooks');
|
|
||||||
|
|
||||||
let indent = 0;
|
let indent = 0;
|
||||||
async_hooks.createHook({
|
async_hooks.createHook({
|
||||||
init(asyncId, type, triggerId) {
|
init(asyncId, type, triggerId) {
|
||||||
const cId = async_hooks.currentId();
|
const cId = async_hooks.currentId();
|
||||||
fs.writeSync(1, ' '.repeat(indent) + `${type}(${asyncId}): ` +
|
const indentStr = ' '.repeat(indent);
|
||||||
`trigger: ${triggerId} scope: ${cId}\n`);
|
fs.writeSync(
|
||||||
|
1,
|
||||||
|
`${indentStr}${type}(${asyncId}): trigger: ${triggerId} scope: ${cId}\n`);
|
||||||
},
|
},
|
||||||
before(asyncId) {
|
before(asyncId) {
|
||||||
fs.writeSync(1, ' '.repeat(indent) + `before: ${asyncId}`);
|
const indentStr = ' '.repeat(indent);
|
||||||
|
fs.writeSync(1, `${indentStr}before: ${asyncId}\n`);
|
||||||
indent += 2;
|
indent += 2;
|
||||||
},
|
},
|
||||||
after(asyncId) {
|
after(asyncId) {
|
||||||
indent -= 2;
|
indent -= 2;
|
||||||
fs.writeSync(1, ' '.repeat(indent) + `after: ${asyncId}`);
|
const indentStr = ' '.repeat(indent);
|
||||||
|
fs.writeSync(1, `${indentStr}after: ${asyncId}\n`);
|
||||||
},
|
},
|
||||||
destroy(asyncId) {
|
destroy(asyncId) {
|
||||||
fs.writeSync(1, ' '.repeat(indent) + `destroy: ${asyncId}`);
|
const indentStr = ' '.repeat(indent);
|
||||||
|
fs.writeSync(1, `${indentStr}destroy: ${asyncId}\n`);
|
||||||
},
|
},
|
||||||
}).enable();
|
}).enable();
|
||||||
|
|
||||||
@ -319,10 +320,10 @@ before: 5
|
|||||||
>>> 4
|
>>> 4
|
||||||
TickObject(9): trigger: 4 scope: 4
|
TickObject(9): trigger: 4 scope: 4
|
||||||
after: 4
|
after: 4
|
||||||
destroy: 4
|
|
||||||
after: 5
|
after: 5
|
||||||
before: 9
|
before: 9
|
||||||
after: 9
|
after: 9
|
||||||
|
destroy: 4
|
||||||
destroy: 9
|
destroy: 9
|
||||||
destroy: 5
|
destroy: 5
|
||||||
```
|
```
|
||||||
@ -395,8 +396,8 @@ For example:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
console.log(async_hooks.currentId()); // 1 - bootstrap
|
console.log(async_hooks.currentId()); // 1 - bootstrap
|
||||||
fs.open(path, (err, fd) => {
|
fs.open(path, 'r', (err, fd) => {
|
||||||
console.log(async_hooks.currentId()); // 2 - open()
|
console.log(async_hooks.currentId()); // 6 - open()
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -427,9 +428,9 @@ For example:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const server = net.createServer((conn) => {
|
const server = net.createServer((conn) => {
|
||||||
// Though the resource that caused (or triggered) this callback to
|
// The resource that caused (or triggered) this callback to be called
|
||||||
// be called was that of the new connection. Thus the return value
|
// was that of the new connection. Thus the return value of triggerId()
|
||||||
// of triggerId() is the ID of "conn".
|
// is the asyncId of "conn".
|
||||||
async_hooks.triggerId();
|
async_hooks.triggerId();
|
||||||
|
|
||||||
}).listen(port, () => {
|
}).listen(port, () => {
|
||||||
@ -462,6 +463,8 @@ will occur and node will abort.
|
|||||||
The following is an overview of the `AsyncResource` API.
|
The following is an overview of the `AsyncResource` API.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
const { AsyncResource } = require('async_hooks');
|
||||||
|
|
||||||
// AsyncResource() is meant to be extended. Instantiating a
|
// AsyncResource() is meant to be extended. Instantiating a
|
||||||
// new AsyncResource() also triggers init. If triggerId is omitted then
|
// new AsyncResource() also triggers init. If triggerId is omitted then
|
||||||
// async_hook.currentId() is used.
|
// async_hook.currentId() is used.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user