@@ -729,6 +729,26 @@ class DBQuery extends AsyncResource {
729729}
730730```
731731
732+ #### ` static AsyncResource.bind(fn[, type]) `
733+ <!-- YAML
734+ added: REPLACEME
735+ -->
736+
737+ * ` fn ` {Function} The function to bind to the current execution context.
738+ * ` type ` {string} An optional name to associate with the underlying
739+ ` AsyncResource ` .
740+
741+ Binds the given function to the current execution context.
742+
743+ #### ` asyncResource.bind(fn) `
744+ <!-- YAML
745+ added: REPLACEME
746+ -->
747+
748+ * ` fn ` {Function} The function to bind to the current ` AsyncResource ` .
749+
750+ Binds the given function to execute to this ` AsyncResource ` 's scope.
751+
732752#### ` asyncResource.runInAsyncScope(fn[, thisArg, ...args]) `
733753<!-- YAML
734754added: v9.6.0
@@ -900,12 +920,12 @@ const { createServer } = require('http');
900920const { AsyncResource , executionAsyncId } = require (' async_hooks' );
901921
902922const server = createServer ((req , res ) => {
903- const asyncResource = new AsyncResource (' request' );
904- // The listener will always run in the execution context of `asyncResource`.
905- req .on (' close' , asyncResource .runInAsyncScope .bind (asyncResource, () => {
906- // Prints: true
907- console .log (asyncResource .asyncId () === executionAsyncId ());
923+ req .on (' close' , AsyncResource .bind (() => {
924+ // Execution context is bound to the current outer scope.
908925 }));
926+ req .on (' close' , () => {
927+ // Execution context is bound to the scope that caused 'close' to emit.
928+ });
909929 res .end ();
910930}).listen (3000 );
911931```
0 commit comments