Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code retry with promises
// Author: Renat Tuktarov (renat@sourcerer.io)
const retry = function(fn, prev) {
return new Promise((current, reject) => {
const resolve = _ => (prev && prev()) || current();
fn(resolve, delay => {
setTimeout(_ => {
retry(fn, resolve);
}, delay);
});
});
};
function run() {
let attempt = 1;
retry((done, retry) => {
console.log('attempt: ', attempt);
if (attempt !== 3) {
attempt += 1;
retry(1000);
} else {
done();
}
});
}
run();
@anders0l

This comment has been minimized.

Copy link

commented Dec 23, 2017

awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.