Last active
March 12, 2019 18:14
-
-
Save pettomartino/2fc131211dcbe444a2940bd9baaed688 to your computer and use it in GitHub Desktop.
Retry promise
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
const retry = (fn, options = {}) => new Promise((resolve, reject) => { | |
const { retryInterval = 500, retries = 1 } = options; | |
fn() | |
.then(resolve) | |
.catch(err => ( | |
(retries === 0) | |
? reject(err) | |
: setTimeout( | |
() => retry(fn, { retryInterval, retries: retries - 1 }).then(resolve, reject), | |
retryInterval, | |
) | |
)); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment