common-stuff
    Preparing search index...

    Function retry

    • Retries an async function up to attempts times.

      backoff controls the delay between attempts in milliseconds — either a fixed number or a function (attemptNumber) => delayMs (1-indexed). Defaults to no delay.

      The error from the last failed attempt is rethrown.

      Type Parameters

      • T

      Parameters

      • fn: () => Promise<T>
      • Optionaloptions: { attempts?: number; backoff?: number | ((attempt: number) => number) }
        • Optionalattempts?: number

          Total number of attempts (≥ 1). Default 3.

        • Optionalbackoff?: number | ((attempt: number) => number)

          Delay between attempts in ms, or a function of attempt number (1-indexed).

      Returns Promise<T>

      await retry(() => fetch('/flaky'), { attempts: 3, backoff: 200 })
      await retry(fetchUser, { attempts: 5, backoff: n => 100 * 2 ** (n - 1) }) // exponential