@@ -922,7 +922,7 @@ added: v13.10.0
922
922
-->
923
923
924
924
Creates a new instance of ` AsyncLocalStorage ` . Store is only provided within a
925
- ` run ` or a ` runSyncAndReturn ` method call.
925
+ ` run ` method call.
926
926
927
927
### ` asyncLocalStorage.disable() `
928
928
<!-- YAML
@@ -931,8 +931,7 @@ added: v13.10.0
931
931
932
932
This method disables the instance of ` AsyncLocalStorage ` . All subsequent calls
933
933
to ` asyncLocalStorage.getStore() ` will return ` undefined ` until
934
- ` asyncLocalStorage.run() ` or ` asyncLocalStorage.runSyncAndReturn() `
935
- is called again.
934
+ ` asyncLocalStorage.run() ` is called again.
936
935
937
936
When calling ` asyncLocalStorage.disable() ` , all current contexts linked to the
938
937
instance will be exited.
@@ -954,8 +953,7 @@ added: v13.10.0
954
953
955
954
This method returns the current store.
956
955
If 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 ` .
959
957
960
958
### ` asyncLocalStorage.enterWith(store) `
961
959
<!-- YAML
@@ -1008,90 +1006,23 @@ added: v13.10.0
1008
1006
* ` callback ` {Function}
1009
1007
* ` ...args ` {any}
1010
1008
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
-
1078
1009
This methods runs a function synchronously within a context and return its
1079
1010
return value. The store is not accessible outside of the callback function or
1080
1011
the asynchronous operations created within the callback.
1081
1012
1082
1013
Optionally, arguments can be passed to the function. They will be passed to
1083
1014
the callback function.
1084
1015
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.
1088
1019
1089
1020
Example:
1090
1021
1091
1022
``` js
1092
1023
const store = { id: 2 };
1093
1024
try {
1094
- asyncLocalStorage .runSyncAndReturn (store, () => {
1025
+ asyncLocalStorage .run (store, () => {
1095
1026
asyncLocalStorage .getStore (); // Returns the store object
1096
1027
throw new Error ();
1097
1028
});
@@ -1101,7 +1032,7 @@ try {
1101
1032
}
1102
1033
```
1103
1034
1104
- ### ` asyncLocalStorage.exitSyncAndReturn (callback[, ...args]) `
1035
+ ### ` asyncLocalStorage.exit (callback[, ...args]) `
1105
1036
<!-- YAML
1106
1037
added: v13.10.0
1107
1038
-->
@@ -1116,17 +1047,17 @@ the asynchronous operations created within the callback.
1116
1047
Optionally, arguments can be passed to the function. They will be passed to
1117
1048
the callback function.
1118
1049
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
1121
1052
the context will be re-entered.
1122
1053
1123
1054
Example:
1124
1055
1125
1056
``` js
1126
- // Within a call to run or runSyncAndReturn
1057
+ // Within a call to run
1127
1058
try {
1128
1059
asyncLocalStorage .getStore (); // Returns the store object or value
1129
- asyncLocalStorage .exitSyncAndReturn (() => {
1060
+ asyncLocalStorage .exit (() => {
1130
1061
asyncLocalStorage .getStore (); // Returns undefined
1131
1062
throw new Error ();
1132
1063
});
@@ -1136,68 +1067,23 @@ try {
1136
1067
}
1137
1068
```
1138
1069
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 `
1185
1071
1186
1072
If, within an async function, only one ` await ` call is to run within a context,
1187
1073
the following pattern should be used:
1188
1074
1189
1075
``` js
1190
1076
async function fn () {
1191
- await asyncLocalStorage .runSyncAndReturn (new Map (), () => {
1077
+ await asyncLocalStorage .run (new Map (), () => {
1192
1078
asyncLocalStorage .getStore ().set (' key' , value);
1193
1079
return foo (); // The return value of foo will be awaited
1194
1080
});
1195
1081
}
1196
1082
```
1197
1083
1198
1084
In 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 ` .
1201
1087
1202
1088
[ `after` callback ] : #async_hooks_after_asyncid
1203
1089
[ `before` callback ] : #async_hooks_before_asyncid
0 commit comments