@@ -893,7 +893,7 @@ function log(...args) {
893
893
}
894
894
895
895
http .createServer ((request , response ) => {
896
- asyncLocalStorage .run (() => {
896
+ asyncLocalStorage .run (new Map (), () => {
897
897
const store = asyncLocalStorage .getStore ();
898
898
store .set (kReq, request);
899
899
someAsyncOperation ((err , result ) => {
@@ -943,27 +943,27 @@ in the current process.
943
943
added: REPLACEME
944
944
-->
945
945
946
- * Returns: {Map }
946
+ * Returns: {any }
947
947
948
948
This method returns the current store.
949
949
If this method is called outside of an asynchronous context initialized by
950
950
calling ` asyncLocalStorage.run ` or ` asyncLocalStorage.runAndReturn ` , it will
951
951
return ` undefined ` .
952
952
953
- ### ` asyncLocalStorage.run(callback[, ...args]) `
953
+ ### ` asyncLocalStorage.run(store, callback[, ...args]) `
954
954
<!-- YAML
955
955
added: REPLACEME
956
956
-->
957
957
958
+ * ` store ` {any}
958
959
* ` callback ` {Function}
959
960
* ` ...args ` {any}
960
961
961
962
Calling ` asyncLocalStorage.run(callback) ` will create a new asynchronous
962
- context.
963
- Within the callback function and the asynchronous operations from the callback,
964
- ` asyncLocalStorage.getStore() ` will return an instance of ` Map ` known as
965
- "the store". This store will be persistent through the following
966
- asynchronous calls.
963
+ context. Within the callback function and the asynchronous operations from
964
+ the callback, ` asyncLocalStorage.getStore() ` will return the object or
965
+ the primitive value passed into the ` store ` argument (known as "the store").
966
+ This store will be persistent through the following asynchronous calls.
967
967
968
968
The callback will be ran asynchronously. Optionally, arguments can be passed
969
969
to the function. They will be passed to the callback function.
@@ -975,10 +975,11 @@ Also, the stacktrace will be impacted by the asynchronous call.
975
975
Example:
976
976
977
977
``` js
978
- asyncLocalStorage .run (() => {
979
- asyncLocalStorage .getStore (); // Returns a Map
978
+ const store = { id: 1 };
979
+ asyncLocalStorage .run (store, () => {
980
+ asyncLocalStorage .getStore (); // Returns the store object
980
981
someAsyncOperation (() => {
981
- asyncLocalStorage .getStore (); // Returns the same Map
982
+ asyncLocalStorage .getStore (); // Returns the same object
982
983
});
983
984
});
984
985
asyncLocalStorage .getStore (); // Returns undefined
@@ -1007,20 +1008,21 @@ Also, the stacktrace will be impacted by the asynchronous call.
1007
1008
Example:
1008
1009
1009
1010
``` js
1010
- asyncLocalStorage .run (() => {
1011
- asyncLocalStorage .getStore (); // Returns a Map
1011
+ asyncLocalStorage .run (' store value ' , () => {
1012
+ asyncLocalStorage .getStore (); // Returns 'store value'
1012
1013
asyncLocalStorage .exit (() => {
1013
1014
asyncLocalStorage .getStore (); // Returns undefined
1014
1015
});
1015
- asyncLocalStorage .getStore (); // Returns the same Map
1016
+ asyncLocalStorage .getStore (); // Returns 'store value'
1016
1017
});
1017
1018
```
1018
1019
1019
- ### ` asyncLocalStorage.runSyncAndReturn(callback[, ...args]) `
1020
+ ### ` asyncLocalStorage.runSyncAndReturn(store, callback[, ...args]) `
1020
1021
<!-- YAML
1021
1022
added: REPLACEME
1022
1023
-->
1023
1024
1025
+ * ` store ` {any}
1024
1026
* ` callback ` {Function}
1025
1027
* ` ...args ` {any}
1026
1028
@@ -1038,9 +1040,10 @@ the context will be exited.
1038
1040
Example:
1039
1041
1040
1042
``` js
1043
+ const store = { id: 2 };
1041
1044
try {
1042
- asyncLocalStorage .runSyncAndReturn (() => {
1043
- asyncLocalStorage .getStore (); // Returns a Map
1045
+ asyncLocalStorage .runSyncAndReturn (store, () => {
1046
+ asyncLocalStorage .getStore (); // Returns the store object
1044
1047
throw new Error ();
1045
1048
});
1046
1049
} catch (e) {
@@ -1073,13 +1076,13 @@ Example:
1073
1076
``` js
1074
1077
// Within a call to run or runSyncAndReturn
1075
1078
try {
1076
- asyncLocalStorage .getStore (); // Returns a Map
1079
+ asyncLocalStorage .getStore (); // Returns the store object or value
1077
1080
asyncLocalStorage .exitSyncAndReturn (() => {
1078
1081
asyncLocalStorage .getStore (); // Returns undefined
1079
1082
throw new Error ();
1080
1083
});
1081
1084
} catch (e) {
1082
- asyncLocalStorage .getStore (); // Returns the same Map
1085
+ asyncLocalStorage .getStore (); // Returns the same object or value
1083
1086
// The error will be caught here
1084
1087
}
1085
1088
```
@@ -1105,8 +1108,9 @@ It cannot be promisified using `util.promisify`. If needed, the `Promise`
1105
1108
constructor can be used:
1106
1109
1107
1110
``` js
1111
+ const store = new Map (); // initialize the store
1108
1112
new Promise ((resolve , reject ) => {
1109
- asyncLocalStorage .run (() => {
1113
+ asyncLocalStorage .run (store, () => {
1110
1114
someFunction ((err , result ) => {
1111
1115
if (err) {
1112
1116
return reject (err);
@@ -1135,7 +1139,7 @@ the following pattern should be used:
1135
1139
1136
1140
``` js
1137
1141
async function fn () {
1138
- await asyncLocalStorage .runSyncAndReturn (() => {
1142
+ await asyncLocalStorage .runSyncAndReturn (new Map (), () => {
1139
1143
asyncLocalStorage .getStore ().set (' key' , value);
1140
1144
return foo (); // The return value of foo will be awaited
1141
1145
});
0 commit comments