Last active
April 19, 2018 20:21
-
-
Save calebdwilliams/adbbb24ed962eb6b3f26574bf06d4ab7 to your computer and use it in GitHub Desktop.
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
function fetchResource(url, interval = 500, i = 1) { | |
let attempts = 0; | |
return new Promise((resolve, reject) => { | |
const repeat = setInterval(() => { | |
fetch(url) | |
.then(response => { | |
if (response.ok) { | |
resolve(response); | |
clearInterval(repeat); | |
} else { | |
throw new Error(response); | |
} | |
}) | |
.catch(error => { | |
if (attempts < i) { | |
attempts += 1; | |
} else { | |
reject(error); | |
clearInterval(repeat); | |
} | |
}); | |
}, interval); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment