Async function to execute
Retry options
Promise resolving to the function's result
This utility handles transient failures by automatically retrying the operation with increasing delays between attempts.
Basic usage:
const result = await withRetry(
() => fetchData('/api/data'),
{ maxRetries: 3 }
);
With custom retry logic:
const result = await withRetry(
() => uploadFile(file),
{
maxRetries: 5,
initialDelay: 500,
isRetryable: (error) => {
// Only retry network errors
return error instanceof TypeError && error.message.includes('fetch');
},
onRetry: (attempt, error, delay) => {
console.log(`Retry ${attempt} after ${delay}ms:`, error);
},
}
);
Execute an async function with exponential backoff retry.