@@ -144,18 +144,20 @@ function destroy(asyncId) { }
144144function promiseResolve (asyncId ) { }
145145```
146146
147- ## ` async_hooks.createHook(callbacks ) `
147+ ## ` async_hooks.createHook(options ) `
148148
149149<!-- YAML
150150added: v8.1.0
151151-->
152152
153- * ` callbacks ` {Object} The [ Hook Callbacks] [ ] to register
153+ * ` options ` {Object} The [ Hook Callbacks] [ ] to register
154154 * ` init ` {Function} The [ ` init ` callback] [ ] .
155155 * ` before ` {Function} The [ ` before ` callback] [ ] .
156156 * ` after ` {Function} The [ ` after ` callback] [ ] .
157157 * ` destroy ` {Function} The [ ` destroy ` callback] [ ] .
158158 * ` promiseResolve ` {Function} The [ ` promiseResolve ` callback] [ ] .
159+ * ` trackPromises ` {boolean} Whether the hook should track ` Promise ` s. Cannot be ` false ` if
160+ ` promiseResolve ` is set. ** Default** : ` true ` .
159161* Returns: {AsyncHook} Instance used for disabling and enabling hooks
160162
161163Registers functions to be called for different lifetime events of each async
@@ -354,7 +356,8 @@ Furthermore users of [`AsyncResource`][] create async resources independent
354356of Node.js itself.
355357
356358There is also the ` PROMISE ` resource type, which is used to track ` Promise `
357- instances and asynchronous work scheduled by them.
359+ instances and asynchronous work scheduled by them. The ` Promise ` s are only
360+ tracked when ` trackPromises ` option is set to ` true ` .
358361
359362Users are able to define their own ` type ` when using the public embedder API.
360363
@@ -910,6 +913,38 @@ only on chained promises. That means promises not created by `then()`/`catch()`
910913will not have the ` before` and ` after` callbacks fired on them. For more details
911914see the details of the V8 [PromiseHooks][] API.
912915
916+ ### Disabling promise execution tracking
917+
918+ Tracking promise execution can cause a significant performance overhead.
919+ To opt out of promise tracking, set ` trackPromises` to ` false ` :
920+
921+ ` ` ` cjs
922+ const { createHook } = require (' node:async_hooks' );
923+ const { writeSync } = require (' node:fs' );
924+ createHook ({
925+ init (asyncId , type , triggerAsyncId , resource ) {
926+ // This init hook does not get called when trackPromises is set to false.
927+ writeSync (1 , ` init hook triggered for ${ type} \n ` );
928+ },
929+ trackPromises: false , // Do not track promises.
930+ }).enable ();
931+ Promise .resolve (1729 );
932+ ` ` `
933+
934+ ` ` ` mjs
935+ import { createHook } from ' node:async_hooks' ;
936+ import { writeSync } from ' node:fs' ;
937+
938+ createHook ({
939+ init (asyncId , type , triggerAsyncId , resource ) {
940+ // This init hook does not get called when trackPromises is set to false.
941+ writeSync (1 , ` init hook triggered for ${ type} \n ` );
942+ },
943+ trackPromises: false , // Do not track promises.
944+ }).enable ();
945+ Promise .resolve (1729 );
946+ ` ` `
947+
913948## JavaScript embedder API
914949
915950Library developers that handle their own asynchronous resources performing tasks
@@ -934,7 +969,7 @@ The documentation for this class has moved [`AsyncLocalStorage`][].
934969[` Worker ` ]: worker_threads.md#class-worker
935970[` after` callback]: #afterasyncid
936971[` before` callback]: #beforeasyncid
937- [` createHook` ]: #async_hookscreatehookcallbacks
972+ [` createHook` ]: #async_hookscreatehookoptions
938973[` destroy` callback]: #destroyasyncid
939974[` executionAsyncResource` ]: #async_hooksexecutionasyncresource
940975[` init` callback]: #initasyncid-type-triggerasyncid-resource
0 commit comments