Skip to content

Instantly share code, notes, and snippets.

@boxpositron
Created August 13, 2020 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxpositron/4e22311dfd59823caa54ca97295306cc to your computer and use it in GitHub Desktop.
Save boxpositron/4e22311dfd59823caa54ca97295306cc to your computer and use it in GitHub Desktop.
const recursionExample = () =>
new Promise((resolve, reject) => {
const maxCycles = 10
let cycle = 0
const run = () => {
try {
if (cycle > maxCycles) {
throw new MaxTriesError(`Max of ${maxCycles} reached`)
}
cycle = cycle + 1
// do action
resolve()
} catch (e) {
// Conditional bind to run here
if (e instanceof MaxTriesError) {
return reject(e)
}
if (e instanceof ExpectedError) {
return run()
}
if (e instanceof UnexpectedError) {
return reject(e)
}
}
}
run()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment