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 need to call res.sendstatus(202) and to block the execution flow. This code is not working because even if my "if statment" is true, "second then block" is logged anyway. I could move the second "then block" into the first one, just after the doanotherAsyncStuff() method is called; but if i do it, i need another catch statment and i'd like to avoid it and to have only one catch statment that is called when an error occours in every async method called. The code is right only if my "if statement" is false.
So my question is: is there a way to block the promise flow execution when my "if statment" is true?
via Radar155
No comments:
Post a Comment