Tweakr
    Preparing search index...

    Function timeout

    • Wraps a promise with a timeout. If the promise does not settle within the specified time, it rejects with a Timeout error.

      Type Parameters

      • T

        The type of the promise's resolved value.

      Parameters

      • promise: Promise<T>

        The promise to wrap with a timeout.

      • ms: number

        Timeout duration in milliseconds.

      • message: string = "Timeout"

        Optional timeout error message (default: "Timeout").

      Returns Promise<Awaited<T>>

      A promise that resolves or rejects like the original promise, or rejects on timeout.

      const result = await timeout(
      new Promise(res => setTimeout(() => res("Done"), 2000)),
      1000
      ).catch(err => err.message);

      console.log(result); // "Timeout"

      1.1.0