(
input: RequestInfo | URL,
init?: RequestInit,
{ retries = 3, backoffMs = 1000 }: { retries?: number; backoffMs?: number } = {},
)
| 635 | status >= 500 || status === 408 || status === 429; |
| 636 | |
| 637 | export const fetchWithRetry = async ( |
| 638 | input: RequestInfo | URL, |
| 639 | init?: RequestInit, |
| 640 | { retries = 3, backoffMs = 1000 }: { retries?: number; backoffMs?: number } = {}, |
| 641 | ): Promise<Response> => { |
| 642 | for (let attempt = 0; attempt <= retries; attempt++) { |
| 643 | try { |
| 644 | const response = await fetch(input, init); |
| 645 | if (response.ok || !isRetryableStatus(response.status) || attempt === retries) { |
| 646 | return response; |
| 647 | } |
| 648 | } catch (error) { |
| 649 | if (attempt === retries) { |
| 650 | throw error; |
| 651 | } |
| 652 | } |
| 653 | await sleep(backoffMs * Math.pow(2, attempt)); |
| 654 | } |
| 655 | // Unreachable, but TypeScript needs it |
| 656 | throw new Error('fetchWithRetry: exhausted retries'); |
| 657 | } |
no test coverage detected