Libraries
    Preparing search index...

    Function withTimeout

    • Executes an async function with a timeout.

      If the function does not resolve within ms milliseconds, the returned promise rejects with a TimeoutError. If an AbortSignal is provided it is aborted on timeout, allowing the wrapped operation to clean up.

      Type Parameters

      • T

        The resolved value type of the wrapped function.

      Parameters

      • fn: (signal: AbortSignal) => Promise<T>

        The async function to execute. Receives an AbortSignal that fires when the timeout elapses.

      • ms: number

        Maximum allowed duration in milliseconds.

      Returns Promise<T>

      The value returned by fn, if it resolves in time.

      TimeoutError if ms elapses before fn resolves.

      import { withTimeout } from '@cleverbrush/async';

      const data = await withTimeout(
      (signal) => fetch('/api/data', { signal }).then(r => r.json()),
      5000
      );