TS-Scribe - v1.2.0
    Preparing search index...

    Function traceFunction

    • Wraps a function to record every invocation — arguments, return value (or error), and duration.

      Works with both synchronous and asynchronous functions. For async functions the TraceCallInfo entry is appended after the Promise settles, and the wrapper transparently propagates the resolved value or rejection. Logging is opt-in via log: true. Errors from TraceOptions.onCall are silently isolated and never reach the caller.

      Type Parameters

      • Args extends unknown[]
      • Return

      Parameters

      • fn: (...args: Args) => Return

        The function to trace. Can be synchronous or asynchronous.

      • options: TraceOptions<Args, Return> = {}

        Configuration options for the tracer.

      Returns TracedFunction<Args, Return>

      A TracedFunction with the same signature as fn, plus a calls array and clear().

      Propagates any error or rejection thrown by fn.

      const traced = traceFunction(fetchUser, { label: 'fetchUser', log: true });

      await traced('user-1');
      await traced('user-2');

      console.log(traced.calls.length); // 2
      console.log(traced.calls[0].durationMs); // e.g. 42.3
      console.log(traced.calls[0].result); // resolved value

      traced.clear(); // reset call history
      // Flag slow calls via onCall
      const traced = traceFunction(computeHash, {
      onCall: (info) => {
      if (info.durationMs > 100) console.warn('slow call', info.args);
      },
      });