Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created April 12, 2019 17:42
Show Gist options
  • Save jcuffe/bb69226ceca19f083e3d1e246e1082b8 to your computer and use it in GitHub Desktop.
Save jcuffe/bb69226ceca19f083e3d1e246e1082b8 to your computer and use it in GitHub Desktop.
Promise retry
// 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