Created
December 10, 2021 10:46
-
-
Save kingisaac95/9ef78638ffb90451eeee2e008bc8dd57 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
function retry(fn, retries = 2, err = null) { | |
console.log("Retries: ", retries); | |
if (!retries) { | |
console.log("Done Retrying..."); | |
return Promise.reject(err); | |
} | |
return fn().catch((err) => { | |
console.log("Retrying..."); | |
return retry(fn, retries - 1, err.response); | |
}); | |
} | |
// Thanks to https://stackoverflow.com/a/51332115/6608075 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment