Are nested catches within promises required? Are nested catches within promises required? mongoose mongoose

Are nested catches within promises required?


No, they won't. They only bubble up to the result promise if you chain your promises, for which you need to return the inner promises created by the callbacks. Otherwise the outer promise cannot wait for them and will not know when/how they resolve (whether they fulfill or reject).

temporaryUserModel.findOne({email: req.body.email}).then(tempUser => {    if (tempUser) {        return temporaryUserModel.findOneAndUpdate({_id: tempUser.toJSON()._id}, user);//      ^^^^^^    } else {        return temporaryUserModel(user).save();//      ^^^^^^    }}).then((doc) => {// no need to duplicate this code when you chain anyway    return res.status(200).json({        status: 'Success',        data: {url: planOpted.chargifySignupUrl}    });}).catch(err => error(err, res));


You can extract some of the logic into separate functions, and return the inner promises to bubble up any exceptions to the promise chain:

temporaryUserModel.findOne({email: req.body.email})  .then(updateTempUser)  .then(formatResponse)  .catch(err => error(err, res));function updateTempUser(tempUser) {  if (tempUser) {    return temporaryUserModel.findOneAndUpdate({        _id: tempUser.toJSON()._id    }, user);  } else {    return temporaryUserModel(user).save()  }}function formatResponse(doc) {  return res.status(200).json({    status: 'Success',    data: {url: planOpted.chargifySignupUrl}  });}