es-toolkit icon indicating copy to clipboard operation
es-toolkit copied to clipboard

Add conditional retry support to `retry` function

Open okinawaa opened this issue 5 months ago • 1 comments

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

  1. Prevents unnecessary retries on permanent failures (e.g., 401, validation errors)
  2. More efficient resource usage
  3. Better error handling patterns

okinawaa avatar Jul 14 '25 09:07 okinawaa

I think that's a good approach 👍

ssi02014 avatar Jul 15 '25 02:07 ssi02014