概述
以下是公共 API 的简单概述。
import async_hooks from 'node:async_hooks';
// 返回当前执行上下文的 ID。
const eid = async_hooks.executionAsyncId();
// 返回负责触发当前
// 执行范围回调的句柄ID。
const tid = async_hooks.triggerAsyncId();
// 创建新的 AsyncHook 实例。所有这些回调都是可选的。
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve });
// 允许调用此 AsyncHook 实例的回调。
// 这不是运行构造函数后的隐式操作,
// 必须显式运行才能开始执行回调。
asyncHook.enable();
// 禁用监听新的异步事件。
asyncHook.disable();
//
// 以下是可以传给 createHook() 的回调。
//
// init() 在对象构造过程中被调用。
// 当此回调运行时,资源可能还没有完成构造。
// 因此,"asyncId" 引用的资源的所有字段可能都没有被填充。
function init(asyncId, type, triggerAsyncId, resource) { }
// before() 在调用资源的回调之前被调用。
// 对于句柄(例如 TCPWrap),它可以被调用 0-N 次,
// 而对于请求(例如 FSReqCallback),它将被调用恰好 1 次。
function before(asyncId) { }
// after() 在资源回调完成后被调用。
function after(asyncId) { }
// destroy() 在销毁资源时被调用。
function destroy(asyncId) { }
// promiseResolve() 仅被 promise 资源调用,
// 当调用传给 Promise 构造函数的 resolve() 函数时
// (直接或通过其他解决 promise 的方式)。
function promiseResolve(asyncId) { }
const async_hooks = require('node:async_hooks');
// 返回当前执行上下文的 ID。
const eid = async_hooks.executionAsyncId();
// 返回负责触发当前
// 执行范围回调的句柄ID。
const tid = async_hooks.triggerAsyncId();
// 创建新的 AsyncHook 实例。所有这些回调都是可选的。
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve });
// 允许调用此 AsyncHook 实例的回调。
// 这不是运行构造函数后的隐式操作,
// 必须显式运行才能开始执行回调。
asyncHook.enable();
// 禁用监听新的异步事件。
asyncHook.disable();
//
// 以下是可以传给 createHook() 的回调。
//
// init() 在对象构造过程中被调用。
// 当此回调运行时,资源可能还没有完成构造。
// 因此,"asyncId" 引用的资源的所有字段可能都没有被填充。
function init(asyncId, type, triggerAsyncId, resource) { }
// before() 在调用资源的回调之前被调用。
// 对于句柄(例如 TCPWrap),它可以被调用 0-N 次,
// 而对于请求(例如 FSReqCallback),它将被调用恰好 1 次。
function before(asyncId) { }
// after() 在资源回调完成后被调用。
function after(asyncId) { }
// destroy() 在销毁资源时被调用。
function destroy(asyncId) { }
// promiseResolve() 仅被 promise 资源调用,
// 当调用传给 Promise 构造函数的 resolve() 函数时
// (直接或通过其他解决 promise 的方式)。
function promiseResolve(asyncId) { }
Following is a simple overview of the public API.
import async_hooks from 'node:async_hooks';
// Return the ID of the current execution context.
const eid = async_hooks.executionAsyncId();
// Return the ID of the handle responsible for triggering the callback of the
// current execution scope to call.
const tid = async_hooks.triggerAsyncId();
// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve });
// Allow callbacks of this AsyncHook instance to call. This is not an implicit
// action after running the constructor, and must be explicitly run to begin
// executing callbacks.
asyncHook.enable();
// Disable listening for new asynchronous events.
asyncHook.disable();
//
// The following are the callbacks that can be passed to createHook().
//
// init() is called during object construction. The resource may not have
// completed construction when this callback runs. Therefore, all fields of the
// resource referenced by "asyncId" may not have been populated.
function init(asyncId, type, triggerAsyncId, resource) { }
// before() is called just before the resource's callback is called. It can be
// called 0-N times for handles (such as TCPWrap), and will be called exactly 1
// time for requests (such as FSReqCallback).
function before(asyncId) { }
// after() is called just after the resource's callback has finished.
function after(asyncId) { }
// destroy() is called when the resource is destroyed.
function destroy(asyncId) { }
// promiseResolve() is called only for promise resources, when the
// resolve() function passed to the Promise constructor is invoked
// (either directly or through other means of resolving a promise).
function promiseResolve(asyncId) { }
const async_hooks = require('node:async_hooks');
// Return the ID of the current execution context.
const eid = async_hooks.executionAsyncId();
// Return the ID of the handle responsible for triggering the callback of the
// current execution scope to call.
const tid = async_hooks.triggerAsyncId();
// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve });
// Allow callbacks of this AsyncHook instance to call. This is not an implicit
// action after running the constructor, and must be explicitly run to begin
// executing callbacks.
asyncHook.enable();
// Disable listening for new asynchronous events.
asyncHook.disable();
//
// The following are the callbacks that can be passed to createHook().
//
// init() is called during object construction. The resource may not have
// completed construction when this callback runs. Therefore, all fields of the
// resource referenced by "asyncId" may not have been populated.
function init(asyncId, type, triggerAsyncId, resource) { }
// before() is called just before the resource's callback is called. It can be
// called 0-N times for handles (such as TCPWrap), and will be called exactly 1
// time for requests (such as FSReqCallback).
function before(asyncId) { }
// after() is called just after the resource's callback has finished.
function after(asyncId) { }
// destroy() is called when the resource is destroyed.
function destroy(asyncId) { }
// promiseResolve() is called only for promise resources, when the
// resolve() function passed to the Promise constructor is invoked
// (either directly or through other means of resolving a promise).
function promiseResolve(asyncId) { }