My code resembles this:
router.post("/", (req, res, next) => {
foo()
.then((result) => {
res.send(result)
})
.catch((error) => {
cosnole.log(error)
})
//I only want the bar() function and everything below it to run if the first promise is rejected and the first .catch function ran
bar()
.then((data) => {
res.send(data)
})
.catch((error) => {
console.log(error)
})
})
I'd like to only run the bar() function and the .then .catch functions after it only if the first promise is rejected and the .catch function fires off.
I've tried this:
router.post("/", (req, res, next) => {
rejected = false
foo()
.then((result) => {
res.send(result)
})
.catch((error) => {
rejected = true
console.log(error)
})
if(rejected == true)
bar()
.then((data) => {
res.send(data)
})
.catch((error) => {
console.log(error)
})
})
but the bar() function never gets executed when the first foo() function's error is caught and the promise is rejected.