异步钩子


【Async hooks】

稳定性: 1 - 实验性。如果可能,请迁移离开此 API。 我们不推荐使用 createHookAsyncHookexecutionAsyncResource API,因为它们存在可用性问题、安全风险和性能影响。异步上下文跟踪的使用场景更适合使用稳定的 AsyncLocalStorage API。如果你有使用 createHookAsyncHookexecutionAsyncResource 的需求,且超出了 AsyncLocalStorage 解决的上下文跟踪需求或 诊断通道 当前提供的诊断数据,请在 https://github.com/nodejs/node/issues 提交问题,描述你的使用场景,以便我们开发更有针对性的 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:】

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');