@consumidor-positivo/ts-utils - v1.13.0
    Preparing search index...

    Function withRetry

    • Performs an asynchronous operation with automatic retries on failure.

      This utility is useful for operations that might fail temporarily due to network issues, rate limits, or other transient errors.

      Type Parameters

      • T

      Parameters

      • operation: () => Promise<T>

        The async function to execute with retry support

      • options: WithRetryOpts

        Configuration options for retry behavior

      Returns Promise<T>

      A promise that resolves with the result of the operation

      import { withRetry } from '@consumidor-positivo/ts-utils';

      // Basic usage with default settings (1 retry)
      try {
      const result = await withRetry(
      async () => await fetchDataFromApi(),
      {}
      );
      console.log('Success:', result);
      } catch (error) {
      console.error('Failed after retrying:', error);
      }

      // Advanced usage with custom options
      const result = await withRetry(
      async () => await fetchDataFromApi(),
      {
      maxRetries: 3,
      onRetry: (error, retriesLeft) => {
      console.log(`Retrying operation. ${retriesLeft} attempts remaining. Error: ${error.message}`);
      }
      }
      );

      The last error encountered if all retries fail