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

    Function timeout

    • Wraps an existing promise with a rejection deadline.

      If the wrapped promise does not settle within ms milliseconds, the returned promise rejects with an AbortError. If the promise settles before the deadline, the timer is cancelled and its result is forwarded transparently.

      Optionally accepts an AbortSignal — if the signal fires before the deadline, the returned promise rejects immediately.


      Example:

      // Reject after 5 seconds
      const data = await timeout(fetch('/api/data'), 5_000);

      Type Parameters

      • T

      Parameters

      • promise: PromiseLike<T>

        The promise to wrap with a deadline.

      • ms: number

        Maximum number of milliseconds to wait before rejecting.

      • Optionaloptions: TimeoutOptions

        Optional configuration.

      Returns Promise<T>

      A promise that resolves with the original value or rejects on timeout.

      // With a custom error message
      const result = await timeout(expensiveQuery(), 3_000, { message: 'Query timed out' });
      // With an AbortSignal
      const controller = new AbortController();
      const result = await timeout(fetch(url), 5_000, { signal: controller.signal });