Skip to content

Instantly share code, notes, and snippets.

@myndzi
Last active August 1, 2016 00:54
Show Gist options
  • Save myndzi/238b143f9e2efe38a22739067c1349d9 to your computer and use it in GitHub Desktop.
Save myndzi/238b143f9e2efe38a22739067c1349d9 to your computer and use it in GitHub Desktop.
function delay(ms) {
// error checking wouldn't hurt
return new Promise(resolve => setTimeout(resolve, delay));
}
function retryWithDelay(fn, delay, retries) {
if (retries <= 0) {
return Promise.reject(new Error('out of retries'));
}
return fn().catch(err => {
// typed catch or checking the kind of error here would be a good thing
if (err.code !== 'the thing you expect') { throw err; }
return delay(delay).then(() =>
retryWithDelay(fn, delay, retries - 1)
);
});
}
function retryWithDelay(fn, delay, retries) {
return Promise.try(() => {
if (retries <= 0) { throw new Error('out of retries'); }
return fn();
}).catch(err => {
// typed catch or checking the kind of error here would be a good thing
if (err.code !== 'the thing you expect') { throw err; }
return Promise.delay(delay).then(() =>
retryWithDelay(fn, delay, retries - 1)
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment