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