Retry a failed request
Try again a few times for network errors and 5xx, but never for 4xx.
AJAX · Retry a failed requestIntermediate
async function fetchRetry(url, options = {}, tries = 3) {
let lastError;
for (let i = 0; i < tries; i++) {
try {
const res = await fetch(url, options);
if (res.status < 500) return res; // success or a client error: do not retry
lastError = new Error('HTTP ' + res.status);
} catch (e) {
lastError = e;
}
await new Promise(r => setTimeout(r, 200 * (i + 1)));
}
throw lastError;
}retryfetchnetworkresilience