🔁 Retries an asynchronous function until it succeeds or reaches the maximum number of attempts.

Syntax

TypeScript
import { retryRun } from '@opentf/std';

retryRun<T>(
  func: () => Promise<T>,
  options?: {
    retries?: number;
    delay?: number;
    backoff?: 'fixed' | 'exponential';
    onRetry?: (error: any, attempt: number) => void;
  }
): Promise<T>;

Parameters

  • func: The asynchronous function to retry.

  • options:

    • retries: The number of times to retry the function. Default: 3.

    • delay: The initial delay in milliseconds between retries. Default: 0.

    • backoff: The strategy for calculating delay between retries. Can be 'fixed' or 'exponential'. Default: 'fixed'.

    • onRetry: A callback invoked on each retry attempt.

retries must be a non-negative integer, delay must be a non-negative finite number, and backoff must be either 'fixed' or 'exponential'.

Returns

A Promise that resolves with the function's result or rejects with the last error.

Examples

TypeScript
const result = await retryRun(() => fetchData(), { 
  retries: 3, 
  delay: 1000,
  backoff: 'exponential' 
});
Last updated on
Edit this page