Created
August 18, 2024 16:04
-
-
Save sugoidogo/8e8426aea28fe4ee805e5db15edd3c01 to your computer and use it in GitHub Desktop.
fetch polyfill with automatic retries in case of network issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {RequestInfo | URL} input | |
* @param {RequestInit} init | |
* @returns {Promise<Response>} | |
*/ | |
function fetch(input,init=undefined){ | |
return window.fetch(input,init).catch(async error=>{ | |
if(error.message==="Failed to fetch"){ | |
await new Promise(function(resolve,reject){ | |
setTimeout(resolve,1000) | |
}) | |
return fetch(input,init) | |
}else{ | |
throw error | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment