I've got the following code:
router.post('/', function(req, res, next) {
doAsyncStuff()
.then(ret=>{
console.log('first then block')
if (/*something*/)
res.sendStatus(202); /*I want to stop the execution here. changing this in return res.sendstatus will not solve the problem*/
else
return doanotherAsyncStuff() /*i could move the second then block here but i need another catch statment*/
})
.then(ret=>{
console.log('second then block');
res.sendStatus(200)
})
.catch(err=>{
console.log(err)
err.status = 503
next(err)
})
});
My problem is that when my if
expression is true, I want to call res.sendstatus(202)
and stop the execution flow. But my code is not doing what I want, because even if my if
expression is true, "second then block" is still logged.
I could move the second then
block into the first one, just after the doanotherAsyncStuff()
method is called, but if I do that I need another catch statement, and I'd like to have only one catch statement that is called when an error occurs in any async method called.
So my question is: is there a way to block the promise flow execution when my if
expression is true?