Promise 钩子


promiseHooks 接口可用于跟踪 promise 生命周期事件。 要跟踪所有的异步活动,则参阅 async_hooks,其在内部使用此模块生成 promise 生命周期事件以及其他异步资源的事件。 请求的上下文管理参阅 AsyncLocalStorage

import { promiseHooks } from 'node:v8';

// promise 产生了四个生命周期事件:

// `init` 事件代表了 promise 的创建。
// 这可以是直接创建,例如使用 `new Promise(...)`,
// 或者是继续,例如 `then()` 或 `catch()`。
// 每当调用异步函数或执行“等待”时,也会发生这种情况。
// 如果创建了继续 promise,则 `parent` 将是它作为继续的 promise。
function init(promise, parent) {
  console.log('a promise was created', { promise, parent });
}

// `settled` 事件在 promise 收到解决或拒绝值时发生。
// 这可能会同步地发生,
// 例如在非 promise 输入上使用 `Promise.resolve()` 时。
function settled(promise) {
  console.log('a promise resolved or rejected', { promise });
}

// `before` 事件在 `then()` 或 `catch()` 句柄运行
// 或 `await` 恢复执行之前立即运行。
function before(promise) {
  console.log('a promise is about to call a then handler', { promise });
}

// `after` 事件在 `then()` 句柄运行之后
// 或 `await` 从另一个句柄恢复之后立即运行。
function after(promise) {
  console.log('a promise is done calling a then handler', { promise });
}

// 生命周期钩子可以单独启动和停止
const stopWatchingInits = promiseHooks.onInit(init);
const stopWatchingSettleds = promiseHooks.onSettled(settled);
const stopWatchingBefores = promiseHooks.onBefore(before);
const stopWatchingAfters = promiseHooks.onAfter(after);

// 或者它们可以分组启动和停止
const stopHookSet = promiseHooks.createHook({
  init,
  settled,
  before,
  after
});

// 要停止钩子,则调用创建时返回的函数。
stopWatchingInits();
stopWatchingSettleds();
stopWatchingBefores();
stopWatchingAfters();
stopHookSet();

The promiseHooks interface can be used to track promise lifecycle events. To track all async activity, see async_hooks which internally uses this module to produce promise lifecycle events in addition to events for other async resources. For request context management, see AsyncLocalStorage.

import { promiseHooks } from 'node:v8';

// There are four lifecycle events produced by promises:

// The `init` event represents the creation of a promise. This could be a
// direct creation such as with `new Promise(...)` or a continuation such
// as `then()` or `catch()`. It also happens whenever an async function is
// called or does an `await`. If a continuation promise is created, the
// `parent` will be the promise it is a continuation from.
function init(promise, parent) {
  console.log('a promise was created', { promise, parent });
}

// The `settled` event happens when a promise receives a resolution or
// rejection value. This may happen synchronously such as when using
// `Promise.resolve()` on non-promise input.
function settled(promise) {
  console.log('a promise resolved or rejected', { promise });
}

// The `before` event runs immediately before a `then()` or `catch()` handler
// runs or an `await` resumes execution.
function before(promise) {
  console.log('a promise is about to call a then handler', { promise });
}

// The `after` event runs immediately after a `then()` handler runs or when
// an `await` begins after resuming from another.
function after(promise) {
  console.log('a promise is done calling a then handler', { promise });
}

// Lifecycle hooks may be started and stopped individually
const stopWatchingInits = promiseHooks.onInit(init);
const stopWatchingSettleds = promiseHooks.onSettled(settled);
const stopWatchingBefores = promiseHooks.onBefore(before);
const stopWatchingAfters = promiseHooks.onAfter(after);

// Or they may be started and stopped in groups
const stopHookSet = promiseHooks.createHook({
  init,
  settled,
  before,
  after
});

// To stop a hook, call the function returned at its creation.
stopWatchingInits();
stopWatchingSettleds();
stopWatchingBefores();
stopWatchingAfters();
stopHookSet();