es-toolkit
es-toolkit copied to clipboard
Add conditional retry support to `retry` function
Currently, the retry function retries on all errors. It would be useful to have conditional retry support based on error type.
Use case
// Want to retry only on network errors, not on validation errors
const data = await retry(() => fetchData(), {
retries: 3,
shouldRetry: (error) => isNetworkError(error) // Only retry if network error
});
Proposed API
interface RetryOptions {
retries?: number;
delay?: number | ((attempt: number) => number);
signal?: AbortSignal;
shouldRetry?: (error: Error) => boolean; // New option
}
Benefits
- Prevents unnecessary retries on permanent failures (e.g., 401, validation errors)
- More efficient resource usage
- Better error handling patterns
I think that's a good approach 👍