@@ -893,7 +893,7 @@ function log(...args) {
893893}
894894
895895http .createServer ((request , response ) => {
896- asyncLocalStorage .run (() => {
896+ asyncLocalStorage .run (new Map (), () => {
897897 const store = asyncLocalStorage .getStore ();
898898 store .set (kReq, request);
899899 someAsyncOperation ((err , result ) => {
@@ -943,27 +943,27 @@ in the current process.
943943added: REPLACEME
944944-->
945945
946- * Returns: {Map }
946+ * Returns: {any }
947947
948948This method returns the current store.
949949If this method is called outside of an asynchronous context initialized by
950950calling ` asyncLocalStorage.run ` or ` asyncLocalStorage.runAndReturn ` , it will
951951return ` undefined ` .
952952
953- ### ` asyncLocalStorage.run(callback[, ...args]) `
953+ ### ` asyncLocalStorage.run(store, callback[, ...args]) `
954954<!-- YAML
955955added: REPLACEME
956956-->
957957
958+ * ` store ` {any}
958959* ` callback ` {Function}
959960* ` ...args ` {any}
960961
961962Calling ` 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.
967967
968968The callback will be ran asynchronously. Optionally, arguments can be passed
969969to the function. They will be passed to the callback function.
@@ -975,10 +975,11 @@ Also, the stacktrace will be impacted by the asynchronous call.
975975Example:
976976
977977``` 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
980981 someAsyncOperation (() => {
981- asyncLocalStorage .getStore (); // Returns the same Map
982+ asyncLocalStorage .getStore (); // Returns the same object
982983 });
983984});
984985asyncLocalStorage .getStore (); // Returns undefined
@@ -1007,20 +1008,21 @@ Also, the stacktrace will be impacted by the asynchronous call.
10071008Example:
10081009
10091010``` js
1010- asyncLocalStorage .run (() => {
1011- asyncLocalStorage .getStore (); // Returns a Map
1011+ asyncLocalStorage .run (' store value ' , () => {
1012+ asyncLocalStorage .getStore (); // Returns 'store value'
10121013 asyncLocalStorage .exit (() => {
10131014 asyncLocalStorage .getStore (); // Returns undefined
10141015 });
1015- asyncLocalStorage .getStore (); // Returns the same Map
1016+ asyncLocalStorage .getStore (); // Returns 'store value'
10161017});
10171018```
10181019
1019- ### ` asyncLocalStorage.runSyncAndReturn(callback[, ...args]) `
1020+ ### ` asyncLocalStorage.runSyncAndReturn(store, callback[, ...args]) `
10201021<!-- YAML
10211022added: REPLACEME
10221023-->
10231024
1025+ * ` store ` {any}
10241026* ` callback ` {Function}
10251027* ` ...args ` {any}
10261028
@@ -1038,9 +1040,10 @@ the context will be exited.
10381040Example:
10391041
10401042``` js
1043+ const store = { id: 2 };
10411044try {
1042- asyncLocalStorage .runSyncAndReturn (() => {
1043- asyncLocalStorage .getStore (); // Returns a Map
1045+ asyncLocalStorage .runSyncAndReturn (store, () => {
1046+ asyncLocalStorage .getStore (); // Returns the store object
10441047 throw new Error ();
10451048 });
10461049} catch (e) {
@@ -1073,13 +1076,13 @@ Example:
10731076``` js
10741077// Within a call to run or runSyncAndReturn
10751078try {
1076- asyncLocalStorage .getStore (); // Returns a Map
1079+ asyncLocalStorage .getStore (); // Returns the store object or value
10771080 asyncLocalStorage .exitSyncAndReturn (() => {
10781081 asyncLocalStorage .getStore (); // Returns undefined
10791082 throw new Error ();
10801083 });
10811084} catch (e) {
1082- asyncLocalStorage .getStore (); // Returns the same Map
1085+ asyncLocalStorage .getStore (); // Returns the same object or value
10831086 // The error will be caught here
10841087}
10851088```
@@ -1105,8 +1108,9 @@ It cannot be promisified using `util.promisify`. If needed, the `Promise`
11051108constructor can be used:
11061109
11071110``` js
1111+ const store = new Map (); // initialize the store
11081112new Promise ((resolve , reject ) => {
1109- asyncLocalStorage .run (() => {
1113+ asyncLocalStorage .run (store, () => {
11101114 someFunction ((err , result ) => {
11111115 if (err) {
11121116 return reject (err);
@@ -1135,7 +1139,7 @@ the following pattern should be used:
11351139
11361140``` js
11371141async function fn () {
1138- await asyncLocalStorage .runSyncAndReturn (() => {
1142+ await asyncLocalStorage .runSyncAndReturn (new Map (), () => {
11391143 asyncLocalStorage .getStore ().set (' key' , value);
11401144 return foo (); // The return value of foo will be awaited
11411145 });
0 commit comments