Skip to content

Instantly share code, notes, and snippets.

@yandzee
Last active September 3, 2020 02:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yandzee/3835c1b0f8f049dc3e99afac000d17e0 to your computer and use it in GitHub Desktop.
Save yandzee/3835c1b0f8f049dc3e99afac000d17e0 to your computer and use it in GitHub Desktop.
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) => {
if (attempt !== 3) {
attempt += 1;
retry(1000);
} else {
done();
}
});
}
run();
@anders0l
Copy link

awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment