Skip to content

Commit

Permalink
Update docs to use executionAsyncResource
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Dec 19, 2019
1 parent e780bb8 commit d0d7522
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions doc/api/async_hooks.md
Expand Up @@ -460,7 +460,7 @@ init for PROMISE with id 6, trigger id: 5 # the Promise returned by then()
after 6
```

#### async_hooks.currentResource()
#### async_hooks.executionAsyncResource()

<!-- YAML
added: REPLACEME
Expand All @@ -472,11 +472,11 @@ added: REPLACEME

```js
const { open } = require('fs');
const { executionAsyncId, currentResource } = require('async_hooks');
const { executionAsyncId, executionAsyncResource } = require('async_hooks');

console.log(executionAsyncId(), currentResource()); // 1 null
console.log(executionAsyncId(), executionAsyncResource()); // 1 null
open(__filename, 'r', (err, fd) => {
console.log(executionAsyncId(), currentResource()); // 7 FSReqWrap
console.log(executionAsyncId(), executionAsyncResource()); // 7 FSReqWrap
});
```

Expand All @@ -487,29 +487,29 @@ using of a tracking `Map` to store the metadata:
const { createServer } = require('http');
const {
executionAsyncId,
currentResource,
executionAsyncResource,
createHook
} = require('async_hooks');
const sym = Symbol('state'); // Private symbol to avoid pollution

createHook({
init(asyncId, type, triggerAsyncId, resource) {
const cr = currentResource();
const cr = executionAsyncResource();
if (cr) {
resource[sym] = cr[sym];
}
}
}).enable();

const server = createServer(function(req, res) {
currentResource()[sym] = { state: req.url };
executionAsyncResource()[sym] = { state: req.url };
setTimeout(function() {
res.end(JSON.stringify(currentResource()[sym]));
res.end(JSON.stringify(executionAsyncResource()[sym]));
}, 100);
}).listen(3000);
```

`currentResource()` will return `null` during application bootstrap.
`executionAsyncResource()` will return `null` during application bootstrap.

#### async_hooks.executionAsyncId()

Expand Down

0 comments on commit d0d7522

Please sign in to comment.