tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])


稳定性: 1 - 实验性

  • fn <Function> 返回 Promise 的函数,用于在其周围封装跟踪
  • context <Object> 共享对象,用于关联跟踪事件
  • thisArg <any> 用于函数调用的接收对象
  • ...args <any> 可选参数,传递给函数
  • 返回:<Promise> 链式来自给定函数返回的 Promise

跟踪返回 Promise 的函数调用。这将在函数执行的同步部分始终产生 start 事件end 事件,并且在到达 Promise 延续时会产生 asyncStart 事件asyncEnd 事件。如果给定的函数抛出错误或返回的 Promise 被拒绝,它也可能产生 error 事件。这将使用 start 通道上的 channel.runStores(context, ...) 运行给定函数,这确保所有事件的任何绑定存储都设置为匹配此跟踪上下文。

【Trace a promise-returning function call. This will always produce a start event and end event around the synchronous portion of the function execution, and will produce an asyncStart event and asyncEnd event when a promise continuation is reached. It may also produce an error event if the given function throws an error or the returned promise rejects. This will run the given function using channel.runStores(context, ...) on the start channel which ensures all events should have any bound stores set to match this trace context.】

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.tracePromise(async () => {
  // Do something
}, {
  some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.tracePromise(async () => {
  // Do something
}, {
  some: 'thing',
});