(
url,
{ maxRetry = 3, delay = 100, ...options } = {}
)
| 19 | * @returns {Promise<Response>} |
| 20 | */ |
| 21 | export const fetchWithRetry = async ( |
| 22 | url, |
| 23 | { maxRetry = 3, delay = 100, ...options } = {} |
| 24 | ) => { |
| 25 | const retries = Math.max(1, Number(maxRetry) || 1); |
| 26 | const backoff = Math.max(0, Number(delay) || 0); |
| 27 | |
| 28 | const attemptFetch = attempt => |
| 29 | fetch(url, { ...options, signal: AbortSignal.timeout(30000) }).catch(e => { |
| 30 | if (attempt === retries || !isTimeoutError(e)) { |
| 31 | throw e; |
| 32 | } |
| 33 | |
| 34 | return sleep(backoff * attempt).then(() => attemptFetch(attempt + 1)); |
| 35 | }); |
| 36 | |
| 37 | return attemptFetch(1); |
| 38 | }; |
no test coverage detected