Skip to content

withRetry

withRetry<T>(fn, options): Promise<T>

Defined in: utils/timing.ts:193

Execute an async function with retry logic.

T

() => Promise<T>

Async function to execute

RetryOptions = {}

Retry options

Promise<T>

Promise that resolves with the function result or rejects after all retries exhausted

// Basic retry with defaults (3 retries, exponential backoff)
const result = await withRetry(() => unreliableApi());
// Custom retry configuration
const result = await withRetry(
() => fetchWithErrors(),
{
maxRetries: 5,
delay: 500,
backoff: "exponential",
shouldRetry: (error) => error.status === 429 || error.status >= 500,
onRetry: (error, attempt, delay) => {
console.log(`Retry ${attempt} after ${delay}ms`);
}
}
);