@@ -922,7 +922,7 @@ added: v13.10.0
922922-->
923923
924924Creates a new instance of ` AsyncLocalStorage ` . Store is only provided within a
925- ` run ` or a ` runSyncAndReturn ` method call.
925+ ` run ` method call.
926926
927927### ` asyncLocalStorage.disable() `
928928<!-- YAML
@@ -931,8 +931,7 @@ added: v13.10.0
931931
932932This method disables the instance of ` AsyncLocalStorage ` . All subsequent calls
933933to ` asyncLocalStorage.getStore() ` will return ` undefined ` until
934- ` asyncLocalStorage.run() ` or ` asyncLocalStorage.runSyncAndReturn() `
935- is called again.
934+ ` asyncLocalStorage.run() ` is called again.
936935
937936When calling ` asyncLocalStorage.disable() ` , all current contexts linked to the
938937instance will be exited.
@@ -954,8 +953,7 @@ added: v13.10.0
954953
955954This method returns the current store.
956955If this method is called outside of an asynchronous context initialized by
957- calling ` asyncLocalStorage.run ` or ` asyncLocalStorage.runSyncAndReturn ` , it will
958- return ` undefined ` .
956+ calling ` asyncLocalStorage.run ` , it will return ` undefined ` .
959957
960958### ` asyncLocalStorage.enterWith(store) `
961959<!-- YAML
@@ -1008,90 +1006,23 @@ added: v13.10.0
10081006* ` callback ` {Function}
10091007* ` ...args ` {any}
10101008
1011- Calling ` asyncLocalStorage.run(callback) ` will create a new asynchronous
1012- context. Within the callback function and the asynchronous operations from
1013- the callback, ` asyncLocalStorage.getStore() ` will return the object or
1014- the primitive value passed into the ` store ` argument (known as "the store").
1015- This store will be persistent through the following asynchronous calls.
1016-
1017- The callback will be ran asynchronously. Optionally, arguments can be passed
1018- to the function. They will be passed to the callback function.
1019-
1020- If an error is thrown by the callback function, it will not be caught by
1021- a ` try/catch ` block as the callback is ran in a new asynchronous resource.
1022- Also, the stacktrace will be impacted by the asynchronous call.
1023-
1024- Example:
1025-
1026- ``` js
1027- const store = { id: 1 };
1028- asyncLocalStorage .run (store, () => {
1029- asyncLocalStorage .getStore (); // Returns the store object
1030- someAsyncOperation (() => {
1031- asyncLocalStorage .getStore (); // Returns the same object
1032- });
1033- });
1034- asyncLocalStorage .getStore (); // Returns undefined
1035- ```
1036-
1037- ### ` asyncLocalStorage.exit(callback[, ...args]) `
1038- <!-- YAML
1039- added: v13.10.0
1040- -->
1041-
1042- * ` callback ` {Function}
1043- * ` ...args ` {any}
1044-
1045- Calling ` asyncLocalStorage.exit(callback) ` will create a new asynchronous
1046- context.
1047- Within the callback function and the asynchronous operations from the callback,
1048- ` asyncLocalStorage.getStore() ` will return ` undefined ` .
1049-
1050- The callback will be ran asynchronously. Optionally, arguments can be passed
1051- to the function. They will be passed to the callback function.
1052-
1053- If an error is thrown by the callback function, it will not be caught by
1054- a ` try/catch ` block as the callback is ran in a new asynchronous resource.
1055- Also, the stacktrace will be impacted by the asynchronous call.
1056-
1057- Example:
1058-
1059- ``` js
1060- asyncLocalStorage .run (' store value' , () => {
1061- asyncLocalStorage .getStore (); // Returns 'store value'
1062- asyncLocalStorage .exit (() => {
1063- asyncLocalStorage .getStore (); // Returns undefined
1064- });
1065- asyncLocalStorage .getStore (); // Returns 'store value'
1066- });
1067- ```
1068-
1069- ### ` asyncLocalStorage.runSyncAndReturn(store, callback[, ...args]) `
1070- <!-- YAML
1071- added: v13.10.0
1072- -->
1073-
1074- * ` store ` {any}
1075- * ` callback ` {Function}
1076- * ` ...args ` {any}
1077-
10781009This methods runs a function synchronously within a context and return its
10791010return value. The store is not accessible outside of the callback function or
10801011the asynchronous operations created within the callback.
10811012
10821013Optionally, arguments can be passed to the function. They will be passed to
10831014the callback function.
10841015
1085- If the callback function throws an error, it will be thrown by
1086- ` runSyncAndReturn ` too. The stacktrace will not be impacted by this call and
1087- the context will be exited.
1016+ If the callback function throws an error, it will be thrown by ` run ` too.
1017+ The stacktrace will not be impacted by this call and the context will
1018+ be exited.
10881019
10891020Example:
10901021
10911022``` js
10921023const store = { id: 2 };
10931024try {
1094- asyncLocalStorage .runSyncAndReturn (store, () => {
1025+ asyncLocalStorage .run (store, () => {
10951026 asyncLocalStorage .getStore (); // Returns the store object
10961027 throw new Error ();
10971028 });
@@ -1101,7 +1032,7 @@ try {
11011032}
11021033```
11031034
1104- ### ` asyncLocalStorage.exitSyncAndReturn (callback[, ...args]) `
1035+ ### ` asyncLocalStorage.exit (callback[, ...args]) `
11051036<!-- YAML
11061037added: v13.10.0
11071038-->
@@ -1116,17 +1047,17 @@ the asynchronous operations created within the callback.
11161047Optionally, arguments can be passed to the function. They will be passed to
11171048the callback function.
11181049
1119- If the callback function throws an error, it will be thrown by
1120- ` exitSyncAndReturn ` too. The stacktrace will not be impacted by this call and
1050+ If the callback function throws an error, it will be thrown by ` exit ` too.
1051+ The stacktrace will not be impacted by this call and
11211052the context will be re-entered.
11221053
11231054Example:
11241055
11251056``` js
1126- // Within a call to run or runSyncAndReturn
1057+ // Within a call to run
11271058try {
11281059 asyncLocalStorage .getStore (); // Returns the store object or value
1129- asyncLocalStorage .exitSyncAndReturn (() => {
1060+ asyncLocalStorage .exit (() => {
11301061 asyncLocalStorage .getStore (); // Returns undefined
11311062 throw new Error ();
11321063 });
@@ -1136,68 +1067,23 @@ try {
11361067}
11371068```
11381069
1139- ### Choosing between ` run ` and ` runSyncAndReturn `
1140-
1141- #### When to choose ` run `
1142-
1143- ` run ` is asynchronous. It is called with a callback function that
1144- runs within a new asynchronous call. This is the most explicit behavior as
1145- everything that is executed within the callback of ` run ` (including further
1146- asynchronous operations) will have access to the store.
1147-
1148- If an instance of ` AsyncLocalStorage ` is used for error management (for
1149- instance, with ` process.setUncaughtExceptionCaptureCallback ` ), only
1150- exceptions thrown in the scope of the callback function will be associated
1151- with the context.
1152-
1153- This method is the safest as it provides strong scoping and consistent
1154- behavior.
1155-
1156- It cannot be promisified using ` util.promisify ` . If needed, the ` Promise `
1157- constructor can be used:
1158-
1159- ``` js
1160- const store = new Map (); // initialize the store
1161- new Promise ((resolve , reject ) => {
1162- asyncLocalStorage .run (store, () => {
1163- someFunction ((err , result ) => {
1164- if (err) {
1165- return reject (err);
1166- }
1167- return resolve (result);
1168- });
1169- });
1170- });
1171- ```
1172-
1173- #### When to choose ` runSyncAndReturn `
1174-
1175- ` runSyncAndReturn ` is synchronous. The callback function will be executed
1176- synchronously and its return value will be returned by ` runSyncAndReturn ` .
1177- The store will only be accessible from within the callback
1178- function and the asynchronous operations created within this scope.
1179- If the callback throws an error, ` runSyncAndReturn ` will throw it and it will
1180- not be associated with the context.
1181-
1182- This method provides good scoping while being synchronous.
1183-
1184- #### Usage with ` async/await `
1070+ ### Usage with ` async/await `
11851071
11861072If, within an async function, only one ` await ` call is to run within a context,
11871073the following pattern should be used:
11881074
11891075``` js
11901076async function fn () {
1191- await asyncLocalStorage .runSyncAndReturn (new Map (), () => {
1077+ await asyncLocalStorage .run (new Map (), () => {
11921078 asyncLocalStorage .getStore ().set (' key' , value);
11931079 return foo (); // The return value of foo will be awaited
11941080 });
11951081}
11961082```
11971083
11981084In this example, the store is only available in the callback function and the
1199- functions called by ` foo ` . Outside of ` runSyncAndReturn ` , calling ` getStore `
1200- will return ` undefined ` .
1085+ functions called by ` foo ` . Outside of ` run ` , calling ` getStore ` will return
1086+ ` undefined ` .
12011087
12021088[ `after` callback ] : #async_hooks_after_asyncid
12031089[ `before` callback ] : #async_hooks_before_asyncid
0 commit comments