-
-
Save myndzi/238b143f9e2efe38a22739067c1349d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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) | |
); | |
}); | |
} |
This file contains hidden or 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 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