| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9eff709 commit 181bd05
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1000,7 +1000,7 @@ added: | |||
| 1000 | 1000 | --> | |
| 1001 | 1001 | ||
| 1002 | 1002 | Creates a new instance of `AsyncLocalStorage`. Store is only provided within a | |
| 1003 | - `run` method call. | ||
| 1003 | + `run` or after `enterWith` method call. | ||
| 1004 | 1004 | ||
| 1005 | 1005 | ### `asyncLocalStorage.disable()` | |
| 1006 | 1006 | <!-- YAML | |
@@ -1011,7 +1011,7 @@ added: | |||
| 1011 | 1011 | ||
| 1012 | 1012 | This method disables the instance of `AsyncLocalStorage`. All subsequent calls | |
| 1013 | 1013 | to `asyncLocalStorage.getStore()` will return `undefined` until | |
| 1014 | - `asyncLocalStorage.run()` is called again. | ||
| 1014 | + `asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()` is called again. | ||
| 1015 | 1015 | ||
| 1016 | 1016 | When calling `asyncLocalStorage.disable()`, all current contexts linked to the | |
| 1017 | 1017 | instance will be exited. | |
@@ -1035,7 +1035,8 @@ added: | |||
| 1035 | 1035 | ||
| 1036 | 1036 | This method returns the current store. | |
| 1037 | 1037 | If this method is called outside of an asynchronous context initialized by | |
| 1038 | - calling `asyncLocalStorage.run`, it will return `undefined`. | ||
| 1038 | + calling `asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()`, it will | ||
| 1039 | + return `undefined`. | ||
| 1039 | 1040 | ||
| 1040 | 1041 | ### `asyncLocalStorage.enterWith(store)` | |
| 1041 | 1042 | <!-- YAML | |
@@ -1046,14 +1047,15 @@ added: | |||
| 1046 | 1047 | ||
| 1047 | 1048 | * `store` {any} | |
| 1048 | 1049 | ||
| 1049 | - Calling `asyncLocalStorage.enterWith(store)` will transition into the context | ||
| 1050 | - for the remainder of the current synchronous execution and will persist | ||
| 1051 | - through any following asynchronous calls. | ||
| 1050 | + This method transitions into the context for the remainder of the current | ||
| 1051 | + synchronous execution and then persists the store through any following | ||
| 1052 | + asynchronous calls. | ||
| 1052 | 1053 | ||
| 1053 | 1054 | Example: | |
| 1054 | 1055 | ||
| 1055 | 1056 | ```js | |
| 1056 | 1057 | const store = { id: 1 }; | |
| 1058 | + // Replaces previous store with the given store object | ||
| 1057 | 1059 | asyncLocalStorage.enterWith(store); | |
| 1058 | 1060 | asyncLocalStorage.getStore(); // Returns the store object | |
| 1059 | 1061 | someAsyncOperation(() => { | |
@@ -1064,7 +1066,9 @@ someAsyncOperation(() => { | |||
| 1064 | 1066 | This transition will continue for the _entire_ synchronous execution. | |
| 1065 | 1067 | This means that if, for example, the context is entered within an event | |
| 1066 | 1068 | handler subsequent event handlers will also run within that context unless | |
| 1067 | - specifically bound to another context with an `AsyncResource`. | ||
| 1069 | + specifically bound to another context with an `AsyncResource`. That is why | ||
| 1070 | + `run` should be preferred over `enterWith` unless there are strong reasons | ||
| 1071 | + to use the latter method. | ||
| 1068 | 1072 | ||
| 1069 | 1073 | ```js | |
| 1070 | 1074 | const store = { id: 1 }; | |
@@ -1130,14 +1134,15 @@ added: | |||
| 1130 | 1134 | ||
| 1131 | 1135 | This methods runs a function synchronously outside of a context and return its | |
| 1132 | 1136 | return value. The store is not accessible within the callback function or | |
| 1133 | - the asynchronous operations created within the callback. | ||
| 1137 | + the asynchronous operations created within the callback, i.e. any `getStore` | ||
| 1138 | + call done within the callback function will always return `undefined`. | ||
| 1134 | 1139 | ||
| 1135 | 1140 | Optionally, arguments can be passed to the function. They will be passed to | |
| 1136 | 1141 | the callback function. | |
| 1137 | 1142 | ||
| 1138 | 1143 | If the callback function throws an error, it will be thrown by `exit` too. | |
| 1139 | - The stacktrace will not be impacted by this call and | ||
| 1140 | - the context will be re-entered. | ||
| 1144 | + The stacktrace will not be impacted by this call and the context will be | ||
| 1145 | + re-entered. | ||
| 1141 | 1146 | ||
| 1142 | 1147 | Example: | |
| 1143 | 1148 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments