Say I have 4 functions: runA()
, runB()
, runC()
and runD()
.
Using ES6 promises, in a completely successful run, these would all be run one after another:
runA()
.then(runB)
.then(runC)
.then(runD)
If runA
or runB
fail (reject or throw), I would like to call error1()
and then completely stop the chain (not call runC
or runD
). This makes me think I should add a single .catch()
at the very end of the .then
promise chain:
runA()
.then(runB)
.then(runC) //won't get called if runA or runB throws
.then(runD) //won't get called if runA or runB throws
.catch(error1)
But if runC
fails, I would like to call error2()
and still stop the chain (not call runD
).
runA()
.then(runB)
.catch(error1) //moved up to only handle runA and runB
.then(runC) //but now this gets called after error1() is run
.then(runD)
.catch(error2)
Now that I have 2 catch
calls in the chain, runC
will get called after error1
is run since the result of the catch will default to a resolve
. Is my only option to have the error1
function create a promise that it always rejects?