@@ -915,7 +915,7 @@ added: REPLACEME
915
915
-->
916
916
917
917
Creates a new instance of ` AsyncLocalStorage ` . Store is only provided within a
918
- ` run ` or a ` runSyncAndReturn ` method call.
918
+ ` run ` method call.
919
919
920
920
### ` asyncLocalStorage.disable() `
921
921
<!-- YAML
@@ -924,8 +924,7 @@ added: REPLACEME
924
924
925
925
This method disables the instance of ` AsyncLocalStorage ` . All subsequent calls
926
926
to ` asyncLocalStorage.getStore() ` will return ` undefined ` until
927
- ` asyncLocalStorage.run() ` or ` asyncLocalStorage.runSyncAndReturn() `
928
- is called again.
927
+ ` asyncLocalStorage.run() ` is called again.
929
928
930
929
When calling ` asyncLocalStorage.disable() ` , all current contexts linked to the
931
930
instance will be exited.
@@ -947,8 +946,7 @@ added: REPLACEME
947
946
948
947
This method returns the current store.
949
948
If this method is called outside of an asynchronous context initialized by
950
- calling ` asyncLocalStorage.run ` or ` asyncLocalStorage.runAndReturn ` , it will
951
- return ` undefined ` .
949
+ calling ` asyncLocalStorage.run ` , it will return ` undefined ` .
952
950
953
951
### ` asyncLocalStorage.enterWith(store) `
954
952
<!-- YAML
@@ -1001,90 +999,23 @@ added: REPLACEME
1001
999
* ` callback ` {Function}
1002
1000
* ` ...args ` {any}
1003
1001
1004
- Calling ` asyncLocalStorage.run(callback) ` will create a new asynchronous
1005
- context. Within the callback function and the asynchronous operations from
1006
- the callback, ` asyncLocalStorage.getStore() ` will return the object or
1007
- the primitive value passed into the ` store ` argument (known as "the store").
1008
- This store will be persistent through the following asynchronous calls.
1009
-
1010
- The callback will be ran asynchronously. Optionally, arguments can be passed
1011
- to the function. They will be passed to the callback function.
1012
-
1013
- If an error is thrown by the callback function, it will not be caught by
1014
- a ` try/catch ` block as the callback is ran in a new asynchronous resource.
1015
- Also, the stacktrace will be impacted by the asynchronous call.
1016
-
1017
- Example:
1018
-
1019
- ``` js
1020
- const store = { id: 1 };
1021
- asyncLocalStorage .run (store, () => {
1022
- asyncLocalStorage .getStore (); // Returns the store object
1023
- someAsyncOperation (() => {
1024
- asyncLocalStorage .getStore (); // Returns the same object
1025
- });
1026
- });
1027
- asyncLocalStorage .getStore (); // Returns undefined
1028
- ```
1029
-
1030
- ### ` asyncLocalStorage.exit(callback[, ...args]) `
1031
- <!-- YAML
1032
- added: REPLACEME
1033
- -->
1034
-
1035
- * ` callback ` {Function}
1036
- * ` ...args ` {any}
1037
-
1038
- Calling ` asyncLocalStorage.exit(callback) ` will create a new asynchronous
1039
- context.
1040
- Within the callback function and the asynchronous operations from the callback,
1041
- ` asyncLocalStorage.getStore() ` will return ` undefined ` .
1042
-
1043
- The callback will be ran asynchronously. Optionally, arguments can be passed
1044
- to the function. They will be passed to the callback function.
1045
-
1046
- If an error is thrown by the callback function, it will not be caught by
1047
- a ` try/catch ` block as the callback is ran in a new asynchronous resource.
1048
- Also, the stacktrace will be impacted by the asynchronous call.
1049
-
1050
- Example:
1051
-
1052
- ``` js
1053
- asyncLocalStorage .run (' store value' , () => {
1054
- asyncLocalStorage .getStore (); // Returns 'store value'
1055
- asyncLocalStorage .exit (() => {
1056
- asyncLocalStorage .getStore (); // Returns undefined
1057
- });
1058
- asyncLocalStorage .getStore (); // Returns 'store value'
1059
- });
1060
- ```
1061
-
1062
- ### ` asyncLocalStorage.runSyncAndReturn(store, callback[, ...args]) `
1063
- <!-- YAML
1064
- added: REPLACEME
1065
- -->
1066
-
1067
- * ` store ` {any}
1068
- * ` callback ` {Function}
1069
- * ` ...args ` {any}
1070
-
1071
1002
This methods runs a function synchronously within a context and return its
1072
1003
return value. The store is not accessible outside of the callback function or
1073
1004
the asynchronous operations created within the callback.
1074
1005
1075
1006
Optionally, arguments can be passed to the function. They will be passed to
1076
1007
the callback function.
1077
1008
1078
- If the callback function throws an error, it will be thrown by
1079
- ` runSyncAndReturn ` too. The stacktrace will not be impacted by this call and
1080
- the context will be exited.
1009
+ If the callback function throws an error, it will be thrown by ` run ` too.
1010
+ The stacktrace will not be impacted by this call and the context will
1011
+ be exited.
1081
1012
1082
1013
Example:
1083
1014
1084
1015
``` js
1085
1016
const store = { id: 2 };
1086
1017
try {
1087
- asyncLocalStorage .runSyncAndReturn (store, () => {
1018
+ asyncLocalStorage .run (store, () => {
1088
1019
asyncLocalStorage .getStore (); // Returns the store object
1089
1020
throw new Error ();
1090
1021
});
@@ -1094,7 +1025,7 @@ try {
1094
1025
}
1095
1026
```
1096
1027
1097
- ### ` asyncLocalStorage.exitSyncAndReturn (callback[, ...args]) `
1028
+ ### ` asyncLocalStorage.exit (callback[, ...args]) `
1098
1029
<!-- YAML
1099
1030
added: REPLACEME
1100
1031
-->
@@ -1109,17 +1040,17 @@ the asynchronous operations created within the callback.
1109
1040
Optionally, arguments can be passed to the function. They will be passed to
1110
1041
the callback function.
1111
1042
1112
- If the callback function throws an error, it will be thrown by
1113
- ` exitSyncAndReturn ` too. The stacktrace will not be impacted by this call and
1043
+ If the callback function throws an error, it will be thrown by ` exit ` too.
1044
+ The stacktrace will not be impacted by this call and
1114
1045
the context will be re-entered.
1115
1046
1116
1047
Example:
1117
1048
1118
1049
``` js
1119
- // Within a call to run or runSyncAndReturn
1050
+ // Within a call to run
1120
1051
try {
1121
1052
asyncLocalStorage .getStore (); // Returns the store object or value
1122
- asyncLocalStorage .exitSyncAndReturn (() => {
1053
+ asyncLocalStorage .exit (() => {
1123
1054
asyncLocalStorage .getStore (); // Returns undefined
1124
1055
throw new Error ();
1125
1056
});
@@ -1129,68 +1060,23 @@ try {
1129
1060
}
1130
1061
```
1131
1062
1132
- ### Choosing between ` run ` and ` runSyncAndReturn `
1133
-
1134
- #### When to choose ` run `
1135
-
1136
- ` run ` is asynchronous. It is called with a callback function that
1137
- runs within a new asynchronous call. This is the most explicit behavior as
1138
- everything that is executed within the callback of ` run ` (including further
1139
- asynchronous operations) will have access to the store.
1140
-
1141
- If an instance of ` AsyncLocalStorage ` is used for error management (for
1142
- instance, with ` process.setUncaughtExceptionCaptureCallback ` ), only
1143
- exceptions thrown in the scope of the callback function will be associated
1144
- with the context.
1145
-
1146
- This method is the safest as it provides strong scoping and consistent
1147
- behavior.
1148
-
1149
- It cannot be promisified using ` util.promisify ` . If needed, the ` Promise `
1150
- constructor can be used:
1151
-
1152
- ``` js
1153
- const store = new Map (); // initialize the store
1154
- new Promise ((resolve , reject ) => {
1155
- asyncLocalStorage .run (store, () => {
1156
- someFunction ((err , result ) => {
1157
- if (err) {
1158
- return reject (err);
1159
- }
1160
- return resolve (result);
1161
- });
1162
- });
1163
- });
1164
- ```
1165
-
1166
- #### When to choose ` runSyncAndReturn `
1167
-
1168
- ` runSyncAndReturn ` is synchronous. The callback function will be executed
1169
- synchronously and its return value will be returned by ` runSyncAndReturn ` .
1170
- The store will only be accessible from within the callback
1171
- function and the asynchronous operations created within this scope.
1172
- If the callback throws an error, ` runSyncAndReturn ` will throw it and it will
1173
- not be associated with the context.
1174
-
1175
- This method provides good scoping while being synchronous.
1176
-
1177
- #### Usage with ` async/await `
1063
+ ### Usage with ` async/await `
1178
1064
1179
1065
If, within an async function, only one ` await ` call is to run within a context,
1180
1066
the following pattern should be used:
1181
1067
1182
1068
``` js
1183
1069
async function fn () {
1184
- await asyncLocalStorage .runSyncAndReturn (new Map (), () => {
1070
+ await asyncLocalStorage .run (new Map (), () => {
1185
1071
asyncLocalStorage .getStore ().set (' key' , value);
1186
1072
return foo (); // The return value of foo will be awaited
1187
1073
});
1188
1074
}
1189
1075
```
1190
1076
1191
1077
In this example, the store is only available in the callback function and the
1192
- functions called by ` foo ` . Outside of ` runSyncAndReturn ` , calling ` getStore `
1193
- will return ` undefined ` .
1078
+ functions called by ` foo ` . Outside of ` run ` , calling ` getStore ` will return
1079
+ ` undefined ` .
1194
1080
1195
1081
[ `after` callback ] : #async_hooks_after_asyncid
1196
1082
[ `before` callback ] : #async_hooks_before_asyncid
0 commit comments