Created
April 12, 2019 17:42
-
-
Save jcuffe/bb69226ceca19f083e3d1e246e1082b8 to your computer and use it in GitHub Desktop.
Promise retry
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
// Common pattern of wrapping a library async function in a promise | |
function retriesAsyncOperation (args, retriesLeft = 5) { | |
return new Promise((resolve, reject) => { | |
performAsyncOp(args) | |
.then(result => resolve(result) | |
.catch((error) => { | |
if (retriesLeft == 0) { | |
reject(error) | |
} else { | |
// Passing reject as second argument allows outermost promise to fail when retries run out | |
retriesAsyncOperation(args, retriesLeft - 1).then(resolve, reject) | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment