@@ -599,10 +599,6 @@ own resources.
599
599
600
600
The ` init ` hook will trigger when an ` AsyncResource ` is instantiated.
601
601
602
- * Note* : ` 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
-
606
602
The following is an overview of the ` AsyncResource ` API.
607
603
608
604
``` js
@@ -615,11 +611,13 @@ const asyncResource = new AsyncResource(
615
611
type, { triggerAsyncId: executionAsyncId (), requireManualDestroy: false }
616
612
);
617
613
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);
623
621
624
622
// Call AsyncHooks destroy callbacks.
625
623
asyncResource .emitDestroy ();
@@ -629,6 +627,14 @@ asyncResource.asyncId();
629
627
630
628
// Return the trigger ID for the AsyncResource instance.
631
629
asyncResource .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 ();
632
638
```
633
639
634
640
#### ` AsyncResource(type[, options]) `
@@ -654,9 +660,7 @@ class DBQuery extends AsyncResource {
654
660
655
661
getInfo (query , callback ) {
656
662
this .db .get (query, (err , data ) => {
657
- this .emitBefore ();
658
- callback (err, data);
659
- this .emitAfter ();
663
+ this .runInAsyncScope (callback, null , err, data);
660
664
});
661
665
}
662
666
@@ -667,15 +671,44 @@ class DBQuery extends AsyncResource {
667
671
}
668
672
```
669
673
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
+
670
689
#### ` asyncResource.emitBefore() `
690
+ <!-- YAML
691
+ deprecated: REPLACEME
692
+ -->
693
+ > Stability: 0 - Deprecated: Use [ ` asyncResource.runInAsyncScope() ` ] [ ] instead.
671
694
672
695
* Returns: {undefined}
673
696
674
697
Call all ` before ` callbacks to notify that a new asynchronous execution context
675
698
is being entered. If nested calls to ` emitBefore() ` are made, the stack of
676
699
` asyncId ` s will be tracked and properly unwound.
677
700
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
+
678
707
#### ` asyncResource.emitAfter() `
708
+ <!-- YAML
709
+ deprecated: REPLACEME
710
+ -->
711
+ > Stability: 0 - Deprecated: Use [ ` asyncResource.runInAsyncScope() ` ] [ ] instead.
679
712
680
713
* Returns: {undefined}
681
714
@@ -686,6 +719,12 @@ If the user's callback throws an exception, `emitAfter()` will automatically be
686
719
called for all ` asyncId ` s on the stack if the error is handled by a domain or
687
720
` 'uncaughtException' ` handler.
688
721
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
+
689
728
#### ` asyncResource.emitDestroy() `
690
729
691
730
* Returns: {undefined}
@@ -705,6 +744,7 @@ never be called.
705
744
constructor.
706
745
707
746
[ `after` callback ] : #async_hooks_after_asyncid
747
+ [ `asyncResource.runInAsyncScope()` ] : #async_hooks_asyncresource_runinasyncscope_fn_thisarg_args
708
748
[ `before` callback ] : #async_hooks_before_asyncid
709
749
[ `destroy` callback ] : #async_hooks_destroy_asyncid
710
750
[ `init` callback ] : #async_hooks_init_asyncid_type_triggerasyncid_resource
0 commit comments