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

    Function retry

    • Retry handler while it throws. Retries are based on the provided options which can define how many retries, delays between retries, and whether retries should continue based on the error.

      Type Parameters

      • TypedValue

      Parameters

      • handler: RetryHandler<TypedValue>

        The function to retry. It should return a value or a promise that resolves to a value.

      • options: RetryOptions = {}

        The configuration options for retrying.

      Returns Promise<TypedValue>

      The result of the handler after it successfully completes.

      Throws the last error encountered if retries are exhausted or if aborted.

      // Basic retry with default options (2 retries, no delay)
      const data = await retry(() => fetch('/api').then(r => r.json()));
      // Retry with custom delays and max retries
      const data = await retry(
      () => fetch('/api').then(r => r.json()),
      { retries: 3, delay: [100, 500, 1000] }
      );
      // Retry with an AbortSignal to cancel
      const controller = new AbortController();
      const data = await retry(
      () => fetch('/api', { signal: controller.signal }).then(r => r.json()),
      { retries: 5, signal: controller.signal }
      );
      // Custom onRetry logic — skip retry for certain errors
      const data = await retry(() => fetch('/api'), {
      retries: 3,
      onRetry: (error, count) => {
      if (error instanceof TypeError) return false; // don't retry network errors
      return count < 3; // retry up to 3 times for other errors
      }
      });