Async Local Storage is a feature introduced in Node.js, initially added in versions v13.10.0 and v12.17.0, and later stabilized in v16.4.0
The feature enables the creation of a shared context that multiple asynchronous functions can access without explicitly passing it
Async Local Storage is neither browser-related nor a typical storage mechanism
A pattern for using AsyncLocalStorage is to initialize an instance of it, which takes a generic type parameter during initialization
Async Local Storage is used in the implementation of Next.js cookies and in building custom contexts in nearly any server framework
Despite its benefits, Async Local Storage may introduce too much 'magic' into the code and importing the context into a module creates a new dependency that needs to be managed
When using Async Local Storage, it is possible to maintain separate contexts for authentication, logging, feature flags, etc.
In a context like authentication, AsyncLocalStorage can help avoid breaking DRY principles and prevent passing values through deeply nested function calls
Async Local Storage can be useful for handling request-based scenarios that need to pass information around between the different parts of a stack
AsyncLocalStorage.run() creates an asynchronous execution context that's passed to all subsequent asynchronous calls within the current stack