JavaScript承诺:拒绝/失败与捕获?

时间:2016-03-03 15:46:46

标签: javascript promise bluebird

我将bluebird用作promise库。

我有两种异常处理模式,无法决定哪一种更好。

这是我拒绝承诺的功能:

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

这是处理异常的两种模式:

1)callback模式

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

2)catch模式

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

我应该使用哪一个?有什么警告?或者我绝对错了,应该采取其他方式吗?

0 个答案:

没有答案
相关问题