介绍


【Introduction】

这些类用于关联状态并在回调和 Promise 链中传播它。它们允许在 Web 请求的整个生命周期或任何其他异步期间存储数据。这类似于其他语言中的线程本地存储。

【These classes are used to associate state and propagate it throughout callbacks and promise chains. They allow storing data throughout the lifetime of a web request or any other asynchronous duration. It is similar to thread-local storage in other languages.】

AsyncLocalStorageAsyncResource 类是 node:async_hooks 模块的一部分:

【The AsyncLocalStorage and AsyncResource classes are part of the node:async_hooks module:】

import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';const { AsyncLocalStorage, AsyncResource } = require('node:async_hooks');