Skip to content

Instantly share code, notes, and snippets.

@calebdwilliams
Last active April 19, 2018 20:21
Show Gist options
  • Save calebdwilliams/adbbb24ed962eb6b3f26574bf06d4ab7 to your computer and use it in GitHub Desktop.
Save calebdwilliams/adbbb24ed962eb6b3f26574bf06d4ab7 to your computer and use it in GitHub Desktop.
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