@@ -900,7 +900,7 @@ function log(...args) {
900900}
901901
902902http .createServer ((request , response ) => {
903- asyncLocalStorage .run (() => {
903+ asyncLocalStorage .run (new Map (), () => {
904904 const store = asyncLocalStorage .getStore ();
905905 store .set (kReq, request);
906906 someAsyncOperation ((err , result ) => {
@@ -950,27 +950,27 @@ in the current process.
950950added: REPLACEME
951951-->
952952
953- * Returns: {Map }
953+ * Returns: {any }
954954
955955This method returns the current store.
956956If this method is called outside of an asynchronous context initialized by
957957calling ` asyncLocalStorage.run ` or ` asyncLocalStorage.runAndReturn ` , it will
958958return ` undefined ` .
959959
960- ### ` asyncLocalStorage.run(callback[, ...args]) `
960+ ### ` asyncLocalStorage.run(store, callback[, ...args]) `
961961<!-- YAML
962962added: REPLACEME
963963-->
964964
965+ * ` store ` {any}
965966* ` callback ` {Function}
966967* ` ...args ` {any}
967968
968969Calling ` asyncLocalStorage.run(callback) ` will create a new asynchronous
969- context.
970- Within the callback function and the asynchronous operations from the callback,
971- ` asyncLocalStorage.getStore() ` will return an instance of ` Map ` known as
972- "the store". This store will be persistent through the following
973- asynchronous calls.
970+ context. Within the callback function and the asynchronous operations from
971+ the callback, ` asyncLocalStorage.getStore() ` will return the object or
972+ the primitive value passed into the ` store ` argument (known as "the store").
973+ This store will be persistent through the following asynchronous calls.
974974
975975The callback will be ran asynchronously. Optionally, arguments can be passed
976976to the function. They will be passed to the callback function.
@@ -982,10 +982,11 @@ Also, the stacktrace will be impacted by the asynchronous call.
982982Example:
983983
984984``` js
985- asyncLocalStorage .run (() => {
986- asyncLocalStorage .getStore (); // Returns a Map
985+ const store = { id: 1 };
986+ asyncLocalStorage .run (store, () => {
987+ asyncLocalStorage .getStore (); // Returns the store object
987988 someAsyncOperation (() => {
988- asyncLocalStorage .getStore (); // Returns the same Map
989+ asyncLocalStorage .getStore (); // Returns the same object
989990 });
990991});
991992asyncLocalStorage .getStore (); // Returns undefined
@@ -1014,20 +1015,21 @@ Also, the stacktrace will be impacted by the asynchronous call.
10141015Example:
10151016
10161017``` js
1017- asyncLocalStorage .run (() => {
1018- asyncLocalStorage .getStore (); // Returns a Map
1018+ asyncLocalStorage .run (' store value ' , () => {
1019+ asyncLocalStorage .getStore (); // Returns 'store value'
10191020 asyncLocalStorage .exit (() => {
10201021 asyncLocalStorage .getStore (); // Returns undefined
10211022 });
1022- asyncLocalStorage .getStore (); // Returns the same Map
1023+ asyncLocalStorage .getStore (); // Returns 'store value'
10231024});
10241025```
10251026
1026- ### ` asyncLocalStorage.runSyncAndReturn(callback[, ...args]) `
1027+ ### ` asyncLocalStorage.runSyncAndReturn(store, callback[, ...args]) `
10271028<!-- YAML
10281029added: REPLACEME
10291030-->
10301031
1032+ * ` store ` {any}
10311033* ` callback ` {Function}
10321034* ` ...args ` {any}
10331035
@@ -1045,9 +1047,10 @@ the context will be exited.
10451047Example:
10461048
10471049``` js
1050+ const store = { id: 2 };
10481051try {
1049- asyncLocalStorage .runSyncAndReturn (() => {
1050- asyncLocalStorage .getStore (); // Returns a Map
1052+ asyncLocalStorage .runSyncAndReturn (store, () => {
1053+ asyncLocalStorage .getStore (); // Returns the store object
10511054 throw new Error ();
10521055 });
10531056} catch (e) {
@@ -1080,13 +1083,13 @@ Example:
10801083``` js
10811084// Within a call to run or runSyncAndReturn
10821085try {
1083- asyncLocalStorage .getStore (); // Returns a Map
1086+ asyncLocalStorage .getStore (); // Returns the store object or value
10841087 asyncLocalStorage .exitSyncAndReturn (() => {
10851088 asyncLocalStorage .getStore (); // Returns undefined
10861089 throw new Error ();
10871090 });
10881091} catch (e) {
1089- asyncLocalStorage .getStore (); // Returns the same Map
1092+ asyncLocalStorage .getStore (); // Returns the same object or value
10901093 // The error will be caught here
10911094}
10921095```
@@ -1112,8 +1115,9 @@ It cannot be promisified using `util.promisify`. If needed, the `Promise`
11121115constructor can be used:
11131116
11141117``` js
1118+ const store = new Map (); // initialize the store
11151119new Promise ((resolve , reject ) => {
1116- asyncLocalStorage .run (() => {
1120+ asyncLocalStorage .run (store, () => {
11171121 someFunction ((err , result ) => {
11181122 if (err) {
11191123 return reject (err);
@@ -1142,7 +1146,7 @@ the following pattern should be used:
11421146
11431147``` js
11441148async function fn () {
1145- await asyncLocalStorage .runSyncAndReturn (() => {
1149+ await asyncLocalStorage .runSyncAndReturn (new Map (), () => {
11461150 asyncLocalStorage .getStore ().set (' key' , value);
11471151 return foo (); // The return value of foo will be awaited
11481152 });
0 commit comments