Tweakr
    Preparing search index...

    Function retryBackoff

    • Retries an asynchronous function using exponential backoff between attempts.

      This is useful for handling transient failures, especially in network or API calls, by progressively increasing the delay between retries.

      Type Parameters

      • T

        The type of the resolved value from the async function.

      Parameters

      • fn: () => Promise<T>

        The asynchronous function to retry.

      • options: RetryBackoffOptions = {}

        Optional configuration for retries, delay, and backoff factor.

      Returns Promise<T>

      A promise resolving to the function's result, or rejecting after all retries fail.

      let attempt = 0;
      const result = await retryBackoff(async () => {
      if (attempt++ < 2) throw new Error("Failed");
      return "Success";
      }, { retries: 5, baseDelay: 100, factor: 2 });

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

      Will throw the last encountered error after exhausting all retries.

      1.1.0