MtracePromise
Bun

method

diagnostics_channel.TracingChannel.tracePromise

tracePromise<ThisArg = any, Args extends any[] = any[], Result extends PromiseLike<unknown> = any>(
fn: (this: ThisArg, ...args: Args) => Result,
context?: ContextType,
thisArg?: ThisArg,
...args: Args
): Result;

Trace an asynchronous function call which returns a Promise or thenable object. 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 the returned promise is resolved or rejected. It may also produce an error event if the given function throws an error or the returned promise is rejected. 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.

If the value returned by fn is not a Promise or thenable, then it will be returned with a warning, and no asyncStart or asyncEnd events will be produced.

To ensure only correct trace graphs are formed, events will only be published if subscribers are present prior to starting the trace. Subscriptions which are added after the trace begins will not receive future events from that trace, only future traces will be seen.

import diagnostics_channel from 'node:diagnostics_channel';

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

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

Function to wrap a trace around

@param context

Shared object to correlate trace events through

@param thisArg

The receiver to be used for the function call

@param args

Optional arguments to pass to the function

@returns

The return value of the given function, or the result of calling .then(...) on the return value if the tracing channel has active subscribers. If the return value is not a Promise or thenable, then it is returned as-is and a warning is emitted.