Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: improve AsyncLocalStorage introduction #36946

Closed
wants to merge 6 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/api/async_hooks.md
Expand Up @@ -973,6 +973,21 @@ chains. It allows storing data throughout the lifetime of a web request
or any other asynchronous duration. It is similar to thread-local storage
in other languages.

## Difference with raw Async Hooks
brillout marked this conversation as resolved.
Show resolved Hide resolved

`AsyncLocalStorage` doesn't provide any new functionality that raw Async Hooks
brillout marked this conversation as resolved.
Show resolved Hide resolved
doesn't already provide: anything implemented with `AsyncLocalStorage` can be
implemented with Async Hooks instead.
brillout marked this conversation as resolved.
Show resolved Hide resolved

The goal of `AsyncLocalStorage` is to provide a more performant and memory safe
brillout marked this conversation as resolved.
Show resolved Hide resolved
way to share state accross asynchronous calls. For example, the
brillout marked this conversation as resolved.
Show resolved Hide resolved
`AsyncLocalStorage` implementation doesn't involve any `destroy` callback,
which considerably improves performance, and, in some cases, `destoy` can not
brillout marked this conversation as resolved.
Show resolved Hide resolved
be called or called very late which usually leads to memory leak in
implementations using it.

## Example

The following example uses `AsyncLocalStorage` to build a simple logger
that assigns IDs to incoming HTTP requests and includes them in messages
logged within each request.
Expand Down