异步钩子
¥Async hooks
createHook、AsyncHook 和 executionAsyncResource API,因为它们存在可用性问题、安全风险和性能影响。稳定的 AsyncLocalStorage API 可以更好地服务于异步上下文跟踪用例。如果你有 createHook、AsyncHook 或 executionAsyncResource 的用例,超出 AsyncLocalStorage 解决的上下文跟踪需求或 诊断通道 当前提供的诊断数据,请在 https://github.com/nodejs/node/issues 上打开一个问题来描述你的用例,以便我们可以创建一个更专注于目的的 API .¥Stability: 1 - Experimental. Please migrate away from this API, if you can.
We do not recommend using the createHook, AsyncHook, and
executionAsyncResource APIs as they have usability issues, safety risks,
and performance implications. Async context tracking use cases are better
served by the stable AsyncLocalStorage API. If you have a use case for
createHook, AsyncHook, or executionAsyncResource beyond the context
tracking need solved by AsyncLocalStorage or diagnostics data currently
provided by Diagnostics Channel, please open an issue at
https://github.com/nodejs/node/issues describing your use case so we can
create a more purpose-focused API.
源代码: lib/async_hooks.js
我们强烈反对使用 async_hooks API。其他可以涵盖其大部分用例的 API 包括:
¥We strongly discourage the use of the async_hooks API.
Other APIs that can cover most of its use cases include:
-
AsyncLocalStorage跟踪异步上下文¥
AsyncLocalStoragetracks async context -
process.getActiveResourcesInfo()跟踪活动资源¥
process.getActiveResourcesInfo()tracks active resources
node:async_hooks 模块提供了 API 来跟踪异步的资源。可以使用以下方式访问它:
¥The node:async_hooks module provides an API to track asynchronous resources.
It can be accessed using:
import async_hooks from 'node:async_hooks';const async_hooks = require('node:async_hooks');