1function fetchRetry(url, options = {}, retries = 3, backoff = 300) {
2 /* 1 */
3 const retryCodes = [408, 500, 502, 503, 504, 522, 524]
4 return fetch(url, options)
5 .then(res => {
6 if (res.ok) return res.json()
7
8 if (retries > 0 && retryCodes.includes(res.status)) {
9 setTimeout(() => {
10 /* 2 */
11 return fetchRetry(url, options, retries - 1, backoff * 2) /* 3 */
12 }, backoff) /* 2 */
13 } else {
14 throw new Error(res)
15 }
16 })
17 .catch(console.error)
18}
19