1

I use bluebird as promise library.

I have two patterns of exception handling, and can not decide, which one is the better.

This is my function which rejects a promise:

var someAsyncMethod = function () {
    return new Promise(function (resolve, reject) {
        reject(new MyUniqueException());
    });
};

These are the two patterns of handling the exception:

1) callback pattern

someAsyncMethod()
    .then(function resolved(){
        // ...
    }, function rejected(exception) {
        // handle exception here
    })
    .catch(function (err) {
        // handle error here
    });

2) catch pattern

someAsyncMethod()
    .then(function () {
        // ...
    })
    .catch(MyUniqueException, function (exception) {
        // handle exception here
    })
    .catch(function (err) {
        // handle error here
    });

Which one should I use? What are the caveats? Or am I absolutely wrong and should do it some other way?

Adam
  • 4,985
  • 2
  • 29
  • 61
  • Those are the same pattern. #1 has two unfiltered exception handlers, which doesn't make much sense, and #2 has one with a filter and one without. – ssube Mar 03 '16 at 15:48
  • #1 pattern should be avoided unless you are trying to catch and rethrow. – MinusFour Mar 03 '16 at 15:52
  • Thanks @MinusFour, but in what cases should anyone use the `reject` callback then? – Adam Mar 03 '16 at 15:57
  • I didn't imply that you can't do that, i just said that it should be avoided. You let the error bubble most of the time and catch it at the end. – MinusFour Mar 03 '16 at 16:03
  • But "should" here implies that it doesn't really have to be. You can have it otherwise. The pattern that you mention can be particularly harmful if not treated careful as it swallows any type of error right there and then. You leave consumers without knowing what the heck happened, unless you rethrow. – MinusFour Mar 03 '16 at 16:18

0 Answers0