Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: James M Snell <[email protected]>
  • Loading branch information
flakey5 and jasnell authored Jan 28, 2023
1 parent 2846886 commit 3aacf44
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions doc/api/async_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ this time will print the stack trace and exit. See
Creating an async resource within the `onPropagate` callback will result in
a recursive call to `onPropagate`.

### `asyncLocalStorage.bind(fn, thisArg)`
### `asyncLocalStorage.bind(fn [, thisArg])`

<!-- YAML
added: REPLACEME
Expand All @@ -167,13 +167,14 @@ the `AsyncResource` to which the function is bound.
added: REPLACEME
-->

Returns a callback that captures the current async context and invokes a callback passed into it within the captured async context.
Returns a callback that captures the current async context and invokes a callback passed into it
within the captured async context.

```js
const asyncLocalStorage = new AsyncLocalStorage();
const runInAsyncScope = asyncLocalStorage.run(123, () => als.snapshot());
const result = asyncLocalStorage.run(321, () => runInAsyncScope(() => asyncLocalStorage.getStore()));
console.log(result) // returns 123
console.log(result); // returns 123
```

AsyncLocalStorage.snapshot() can replace the use of AsyncResource for simple async context tracking purposes, for example:
Expand Down

0 comments on commit 3aacf44

Please sign in to comment.