The async function to execute with retry support
Configuration options for retry behavior
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}`);
}
}
);
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.