In order for this question to be useful to as many people as possible, I would exclude my specific implementation details, in addition to using the Bluebird promise library with Node + Express below.
So, let's say that I have the following chain (where P returns the promise and res is the express HTTP response object):
P().then(function(){ // do nothing if all went well (for now) // we only care if there is an error }).catch(function(error){ res.status(500).send("An error occurred"); }).then(function(){ return P(); }).then(function(pVal1){ return [pVal1, P()]; }) // TODO: catch an error from P() here and log pVal1 .spread(function(pVal1, pVal2){ if(pVal1 === pVal2) { console.log("Success!"); } else { console.log("Failure"); } });
Where I posted a TODO comment above, where I would like to catch an error that could occur from my call to P If I catch an error, I would like to register pVal1 , and then send a 500 error, as is done in the first catch. However, I'm not sure if this is possible with the way I structure my chain.
I think I need to do a fork, but I donβt think I understand this concept well enough to stop the asynchronous nature of JavaScript in order to get the best out of me! As such, any help is fully appreciated.
source share