Skip to content

Commit db3d6b3

Browse files
committed
doc: add snippet for AsyncResource and EE integration
PR-URL: #33751 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vladimir de Turckheim <[email protected]>
1 parent cf32b4c commit db3d6b3

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

doc/api/async_hooks.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ createHook({
509509
}
510510
}).enable();
511511

512-
const server = createServer(function(req, res) {
512+
const server = createServer((req, res) => {
513513
executionAsyncResource()[sym] = { state: req.url };
514514
setTimeout(function() {
515515
res.end(JSON.stringify(executionAsyncResource()[sym]));
@@ -864,6 +864,31 @@ for (let i = 0; i < 10; i++) {
864864
}
865865
```
866866

867+
### Integrating `AsyncResource` with `EventEmitter`
868+
869+
Event listeners triggered by an [`EventEmitter`][] may be run in a different
870+
execution context than the one that was active when `eventEmitter.on()` was
871+
called.
872+
873+
The following example shows how to use the `AsyncResource` class to properly
874+
associate an event listener with the correct execution context. The same
875+
approach can be applied to a [`Stream`][] or a similar event-driven class.
876+
877+
```js
878+
const { createServer } = require('http');
879+
const { AsyncResource, executionAsyncId } = require('async_hooks');
880+
881+
const server = createServer((req, res) => {
882+
const asyncResource = new AsyncResource('request');
883+
// The listener will always run in the execution context of `asyncResource`.
884+
req.on('close', asyncResource.runInAsyncScope.bind(asyncResource, () => {
885+
// Prints: true
886+
console.log(asyncResource.asyncId() === executionAsyncId());
887+
}));
888+
res.end();
889+
}).listen(3000);
890+
```
891+
867892
## Class: `AsyncLocalStorage`
868893
<!-- YAML
869894
added:
@@ -1116,8 +1141,10 @@ to associate the asynchronous operation with the correct execution context.
11161141
[`destroy` callback]: #async_hooks_destroy_asyncid
11171142
[`init` callback]: #async_hooks_init_asyncid_type_triggerasyncid_resource
11181143
[`promiseResolve` callback]: #async_hooks_promiseresolve_asyncid
1144+
[`EventEmitter`]: events.html#events_class_eventemitter
11191145
[Hook Callbacks]: #async_hooks_hook_callbacks
11201146
[PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit
1147+
[`Stream`]: stream.html#stream_stream
11211148
[`Worker`]: worker_threads.html#worker_threads_class_worker
11221149
[promise execution tracking]: #async_hooks_promise_execution_tracking
11231150
[`util.promisify()`]: util.html#util_util_promisify_original

0 commit comments

Comments
 (0)