45

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?

3
  • You haven't said why you want to remove the inner catches, but I suggest looking here-- > bluebirdjs.com/docs/api/catch.html and here --> bluebirdjs.com/docs/api/promise.all.html
    – Gojira
    Commented Dec 26, 2016 at 22:21
  • 3
    you need to return your inner promises, then you can bubble up your exceptions Commented Dec 26, 2016 at 22:21
  • 3
    rejected promises and thus .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.
    – jfriend00
    Commented Dec 26, 2016 at 23:13

2 Answers 2

42

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));
2
  • Would this work? return temporaryUserModel.findOneAndUpdate({_id: tempUser.toJSON()._id}, user).then(()=>{console.log(1)}); Meaning... will it still work if I return it and the "then" is on the end? Commented Dec 26, 2016 at 22:36
  • Yes, ,then(…) returns a promise which you also can return from inside the callback. However notice that if you only console.log, the fulfillment value will become undefined.
    – Bergi
    Commented Dec 26, 2016 at 22:38
10

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}
  });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.