@@ -147,6 +147,49 @@ this time will print the stack trace and exit. See
147
147
Creating an async resource within the ` onPropagate ` callback will result in
148
148
a recursive call to ` onPropagate ` .
149
149
150
+ ### Static method: ` AsyncLocalStorage.bind(fn) `
151
+
152
+ <!-- YAML
153
+ added: REPLACEME
154
+ -->
155
+
156
+ * ` fn ` {Function} The function to bind to the current execution context.
157
+
158
+ Binds the given function to the current execution context.
159
+
160
+ The returned function will have an ` asyncResource ` property referencing
161
+ the ` AsyncResource ` to which the function is bound.
162
+
163
+ ### Static method: ` AsyncLocalStorage.snapshot() `
164
+
165
+ <!-- YAML
166
+ added: REPLACEME
167
+ -->
168
+
169
+ Returns a callback that captures the current async context and invokes a
170
+ callback passed into it within the captured async context.
171
+
172
+ ``` js
173
+ const asyncLocalStorage = new AsyncLocalStorage ();
174
+ const runInAsyncScope = asyncLocalStorage .run (123 , () => als .snapshot ());
175
+ const result = asyncLocalStorage .run (321 , () => runInAsyncScope (() => asyncLocalStorage .getStore ()));
176
+ console .log (result); // returns 123
177
+ ```
178
+
179
+ AsyncLocalStorage.snapshot() can replace the use of AsyncResource for simple
180
+ async context tracking purposes, for example:
181
+
182
+ ``` js
183
+ class Foo {
184
+ #runInAsyncScope = AsyncLocalStorage .snapshot ();
185
+
186
+ get () { return this .#runInAsyncScope (() => asyncLocalStorage .getStore ()); }
187
+ }
188
+
189
+ const foo = asyncLocalStorage .run (123 , () => new Foo ());
190
+ console .log (asyncLocalStorage .run (321 , () => foo .get ())); // returns 123
191
+ ```
192
+
150
193
### ` asyncLocalStorage.disable() `
151
194
152
195
<!-- YAML
0 commit comments