Skip to content

Instantly share code, notes, and snippets.

@sugoidogo
Created August 18, 2024 16:04
Show Gist options
  • Save sugoidogo/8e8426aea28fe4ee805e5db15edd3c01 to your computer and use it in GitHub Desktop.
Save sugoidogo/8e8426aea28fe4ee805e5db15edd3c01 to your computer and use it in GitHub Desktop.
fetch polyfill with automatic retries in case of network issues
/**
* @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