Created
August 13, 2020 22:21
-
-
Save boxpositron/4e22311dfd59823caa54ca97295306cc to your computer and use it in GitHub Desktop.
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 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