Performs an HTTP request with automatic retries and timeout.
The request URL (string or URL object).
Configuration options extending standard RequestInit with retry settings.
Optional predicate that overrides the retryOn value from options.
If provided, this function is used instead of options.retryOn.
A Response object from the first successful request (i.e., a response that either
satisfies response.ok === true or for which the retry predicate returns false).
With code NETWORK_ERROR when all attempts fail, or when the last error
cannot be classified otherwise. The original error is preserved as the cause.
Retry conditions:
ok (i.e., response.ok === false) and the retry predicate returns true.Retry mechanism:
retryDelayMs * 2^attempt.AbortController.false, the response is returned immediately (no further retries).Default behavior:
Important: The signal option is not allowed because it is used internally.
If you need custom abort logic, wrap this function with your own AbortController.
// Basic usage with default retry (5xx only)
const res = await fetchWithRetry('https://api.example.com/data');
// Custom retry for any non‑200 response, with more retries
const res = await fetchWithRetry('/api/login', {
method: 'POST',
body: JSON.stringify({ user: 'admin' }),
headers: { 'Content-Type': 'application/json' },
retryOn: (res) => !res.ok,
maxRetries: 5,
timeoutMs: 10000
});
// Override retry predicate via third parameter
const res = await fetchWithRetry(
'https://api.example.com/data',
{ maxRetries: 2 },
(res) => res.status === 429 // retry only on rate limiting
);
HTTP client with automatic retries and timeout support.
Remarks
Wraps
fetchwith exponential backoff retries and per‑request timeout.Param: url
The request URL.
Param: options
Configuration options (timeout, retries, headers, etc.).
Param: shouldRetry
Optional predicate to override
retryOn.Returns
A
Responseobject from the first successful request.Throws
With code
NETWORK_ERRORwhen all attempts fail.See
Since
0.1.1