@@ -599,10 +599,6 @@ own resources.
599599
600600The ` init ` hook will trigger when an ` AsyncResource ` is instantiated.
601601
602- The ` before ` and ` after ` calls must be unwound in the same order that they
603- are called. Otherwise, an unrecoverable exception will occur and the process
604- will abort.
605-
606602The following is an overview of the ` AsyncResource ` API.
607603
608604``` js
@@ -615,11 +611,13 @@ const asyncResource = new AsyncResource(
615611 type, { triggerAsyncId: executionAsyncId (), requireManualDestroy: false }
616612);
617613
618- // Call AsyncHooks before callbacks.
619- asyncResource .emitBefore ();
620-
621- // Call AsyncHooks after callbacks.
622- asyncResource .emitAfter ();
614+ // Run a function in the execution context of the resource. This will
615+ // * establish the context of the resource
616+ // * trigger the AsyncHooks before callbacks
617+ // * call the provided function `fn` with the supplied arguments
618+ // * trigger the AsyncHooks after callbacks
619+ // * restore the original execution context
620+ asyncResource .runInAsyncScope (fn, thisArg, ... args);
623621
624622// Call AsyncHooks destroy callbacks.
625623asyncResource .emitDestroy ();
@@ -629,6 +627,14 @@ asyncResource.asyncId();
629627
630628// Return the trigger ID for the AsyncResource instance.
631629asyncResource .triggerAsyncId ();
630+
631+ // Call AsyncHooks before callbacks.
632+ // Deprecated: Use asyncResource.runInAsyncScope instead.
633+ asyncResource .emitBefore ();
634+
635+ // Call AsyncHooks after callbacks.
636+ // Deprecated: Use asyncResource.runInAsyncScope instead.
637+ asyncResource .emitAfter ();
632638```
633639
634640#### ` AsyncResource(type[, options]) `
@@ -654,9 +660,7 @@ class DBQuery extends AsyncResource {
654660
655661 getInfo (query , callback ) {
656662 this .db .get (query, (err , data ) => {
657- this .emitBefore ();
658- callback (err, data);
659- this .emitAfter ();
663+ this .runInAsyncScope (callback, null , err, data);
660664 });
661665 }
662666
@@ -667,15 +671,44 @@ class DBQuery extends AsyncResource {
667671}
668672```
669673
674+ #### ` asyncResource.runInAsyncScope(fn[, thisArg, ...args]) `
675+ <!-- YAML
676+ added: REPLACEME
677+ -->
678+
679+ * ` fn ` {Function} The function to call in the execution context of this async
680+ resource.
681+ * ` thisArg ` {any} The receiver to be used for the function call.
682+ * ` ...args ` {any} Optional arguments to pass to the function.
683+
684+ Call the provided function with the provided arguments in the execution context
685+ of the async resource. This will establish the context, trigger the AsyncHooks
686+ before callbacks, call the function, trigger the AsyncHooks after callbacks, and
687+ then restore the original execution context.
688+
670689#### ` asyncResource.emitBefore() `
690+ <!-- YAML
691+ deprecated: REPLACEME
692+ -->
693+ > Stability: 0 - Deprecated: Use [ ` asyncResource.runInAsyncScope() ` ] [ ] instead.
671694
672695* Returns: {undefined}
673696
674697Call all ` before ` callbacks to notify that a new asynchronous execution context
675698is being entered. If nested calls to ` emitBefore() ` are made, the stack of
676699` asyncId ` s will be tracked and properly unwound.
677700
701+ ` before ` and ` after ` calls must be unwound in the same order that they
702+ are called. Otherwise, an unrecoverable exception will occur and the process
703+ will abort. For this reason, the ` emitBefore ` and ` emitAfter ` APIs are
704+ considered deprecated. Please use ` runInAsyncScope ` , as it provides a much safer
705+ alternative.
706+
678707#### ` asyncResource.emitAfter() `
708+ <!-- YAML
709+ deprecated: REPLACEME
710+ -->
711+ > Stability: 0 - Deprecated: Use [ ` asyncResource.runInAsyncScope() ` ] [ ] instead.
679712
680713* Returns: {undefined}
681714
@@ -686,6 +719,12 @@ If the user's callback throws an exception, `emitAfter()` will automatically be
686719called for all ` asyncId ` s on the stack if the error is handled by a domain or
687720` 'uncaughtException' ` handler.
688721
722+ ` before ` and ` after ` calls must be unwound in the same order that they
723+ are called. Otherwise, an unrecoverable exception will occur and the process
724+ will abort. For this reason, the ` emitBefore ` and ` emitAfter ` APIs are
725+ considered deprecated. Please use ` runInAsyncScope ` , as it provides a much safer
726+ alternative.
727+
689728#### ` asyncResource.emitDestroy() `
690729
691730* Returns: {undefined}
@@ -705,6 +744,7 @@ never be called.
705744constructor.
706745
707746[ `after` callback ] : #async_hooks_after_asyncid
747+ [ `asyncResource.runInAsyncScope()` ] : #async_hooks_asyncresource_runinasyncscope_fn_thisarg_args
708748[ `before` callback ] : #async_hooks_before_asyncid
709749[ `destroy` callback ] : #async_hooks_destroy_asyncid
710750[ `init` callback ] : #async_hooks_init_asyncid_type_triggerasyncid_resource
0 commit comments