We would like to reduce the number of catch blocks inside our promises. If we remove the nested catches, will exceptions bubble up to the parent catch?
temporaryUserModel.findOne({email: req.body.email})
.then(tempUser => {
if (tempUser) {
temporaryUserModel.findOneAndUpdate({_id: tempUser.toJSON()._id}, user)
.then((doc) => {
return res.status(200).json({
status: 'Success',
data: {url: planOpted.chargifySignupUrl}
});
})
.catch(err => error(err, res));
} else {
temporaryUserModel(user).save()
.then((doc) => {
return res.status(200).json({
status: 'Success',
data: {url: planOpted.chargifySignupUrl}
});
})
.catch(err => error(err, res));
}
})
.catch(err => error(err, res));
We'd like to remove the two nested catches and keep only the catch at the bottom. Is this ok?
.catch()
will bubble up the promise chain if and only if you are returning the inner promises from then.then()
handler which you are not. That's the only way the inner promises are attached to the outer chain.