-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
doc: fix nits in code examples of async_hooks.md #13400
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e06ae82
doc: make require() consistent in async_hooks.md
vsemozhetbyt 16261b3
doc: add missing argument in async_hooks.md
vsemozhetbyt 0845f90
doc: add missing \n in outputs from async_hooks.md
vsemozhetbyt 148f815
doc: reduce string concatenation in async_hooks.md
vsemozhetbyt a12ba9d
doc: update outputs in async_hooks.md
vsemozhetbyt af9dfc0
doc: reword and fix typo in async_hooks.md
vsemozhetbyt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,13 +220,11 @@ while `triggerId` shows *why* a resource was created. | |
The following is a simple demonstration of `triggerId`: | ||
|
||
```js | ||
const async_hooks = require('async_hooks'); | ||
|
||
async_hooks.createHook({ | ||
init(asyncId, type, triggerId) { | ||
const cId = async_hooks.currentId(); | ||
fs.writeSync(1, `${type}(${asyncId}): ` + | ||
`trigger: ${triggerId} scope: ${cId}\n`); | ||
fs.writeSync( | ||
1, `${type}(${asyncId}): trigger: ${triggerId} scope: ${cId}\n`); | ||
} | ||
}).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. | ||
|
||
```js | ||
const async_hooks = require('async_hooks'); | ||
|
||
let indent = 0; | ||
async_hooks.createHook({ | ||
init(asyncId, type, triggerId) { | ||
const cId = async_hooks.currentId(); | ||
fs.writeSync(1, ' '.repeat(indent) + `${type}(${asyncId}): ` + | ||
`trigger: ${triggerId} scope: ${cId}\n`); | ||
const indentStr = ' '.repeat(indent); | ||
fs.writeSync( | ||
1, | ||
`${indentStr}${type}(${asyncId}): trigger: ${triggerId} scope: ${cId}\n`); | ||
}, | ||
before(asyncId) { | ||
fs.writeSync(1, ' '.repeat(indent) + `before: ${asyncId}`); | ||
const indentStr = ' '.repeat(indent); | ||
fs.writeSync(1, `${indentStr}before: ${asyncId}\n`); | ||
indent += 2; | ||
}, | ||
after(asyncId) { | ||
indent -= 2; | ||
fs.writeSync(1, ' '.repeat(indent) + `after: ${asyncId}`); | ||
const indentStr = ' '.repeat(indent); | ||
fs.writeSync(1, `${indentStr}after: ${asyncId}\n`); | ||
}, | ||
destroy(asyncId) { | ||
fs.writeSync(1, ' '.repeat(indent) + `destroy: ${asyncId}`); | ||
const indentStr = ' '.repeat(indent); | ||
fs.writeSync(1, `${indentStr}destroy: ${asyncId}\n`); | ||
}, | ||
}).enable(); | ||
|
||
|
@@ -319,10 +320,10 @@ before: 5 | |
>>> 4 | ||
TickObject(9): trigger: 4 scope: 4 | ||
after: 4 | ||
destroy: 4 | ||
after: 5 | ||
before: 9 | ||
after: 9 | ||
destroy: 4 | ||
destroy: 9 | ||
destroy: 5 | ||
``` | ||
|
@@ -395,8 +396,8 @@ For example: | |
|
||
```js | ||
console.log(async_hooks.currentId()); // 1 - bootstrap | ||
fs.open(path, (err, fd) => { | ||
console.log(async_hooks.currentId()); // 2 - open() | ||
fs.open(path, 'r', (err, fd) => { | ||
console.log(async_hooks.currentId()); // 6 - open() | ||
}); | ||
``` | ||
|
||
|
@@ -427,9 +428,9 @@ For example: | |
|
||
```js | ||
const server = net.createServer((conn) => { | ||
// Though the resource that caused (or triggered) this callback to | ||
// be called was that of the new connection. Thus the return value | ||
// of triggerId() is the ID of "conn". | ||
// The resource that caused (or triggered) this callback to be called | ||
// was that of the new connection. Thus the return value of triggerId() | ||
// is the asyncId of "conn". | ||
async_hooks.triggerId(); | ||
|
||
}).listen(port, () => { | ||
|
@@ -462,6 +463,8 @@ will occur and node will abort. | |
The following is an overview of the `AsyncResource` API. | ||
|
||
```js | ||
const { AsyncResource } = require('async_hooks'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. less (because of the destructuring) but still cruft There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only place where a reader can find out how to obtain |
||
|
||
// AsyncResource() is meant to be extended. Instantiating a | ||
// new AsyncResource() also triggers init. If triggerId is omitted then | ||
// async_hook.currentId() is used. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, something is wrong with the
async_hooks.currentId()
this is supposed to be2
.edit: oh, it might be because of
console.log
. Then6
is likely correct.