承诺拒绝功能

时间:2014-04-02 18:34:00

标签: javascript firefox firefox-addon promise

在javascript中如何拒绝承诺?简单地返回new Error('blah')无法正常工作,错误控制台抛出"。然后它不是一个函数"或类似的东西。

所以我包含了Promise.jsm然后做了return Promise.reject(new Error('blah'))所以我的问题是:如果我在一个承诺中:那么我不必返回Promise.rject并且可以只返回新的错误吗?

 //mainPromise chains to promise0 and chains to promise1
function doPromise(rejectRightAway) {
    if (rejectRightAway) {
        return Promise.reject(new Error('intending to throw mainPromise rejection')); //is this right place to use Promise.reject? //returning just new Error throws in error console, something like: '.then is not a function' and points to the onReject function of mainPromise.then
    }

    promise0.then(
        function () {

            var promise1 = somePromiseObject;
            promise1.then(
                function () {
                    alert('promise1 success');
                },
                function (aRejReason) {
                    alert('promise1 rejected ' + aRejReason.message);
                    return new Error('promise1 rejected so through promise0 rej then mainPromise rej'); //want to throw promise1 rejection //ok to just return new Error?
                }
            );
        },
        function (aRejReason) {
            alert('promise0 rejected with reason = ' + aRejReason.message);
        }
    );

    return promise0;

}

var mainPromise = doPromise();
mainPromise.then(
    function () {
        alert('mainPromise success');
    },
    function (aRejectReason) {
        alert('mainPromise rejected with reason = ' + aRejectReason.message)
    }
);

我不确定这段代码是否有效,所以我的问题是如何拒绝---比如何时返回Promise.reject(new Error())以及何时只是new Error()

2 个答案:

答案 0 :(得分:5)

TL:博士

  • 为了表示操作的正常完成,您将返回其结果return result
  • 为了表示异常,您throw new Error(reason)就像在同步代码中一样,承诺是安全的。
  • 可以明确地从链中返回拒绝,但通常不需要。

Promises的一大优点是它们可以修复异步代码中的异常处理。

承诺不仅仅是一个<行话>组成顺序monadic DSL< /行话>对并发的抽象 - 它们也是安全的!

在这方面,投掷安全真的很棒,你可以像顺序代码一样行事:

function foo(){
    if(somethingWrong){
        // instead of returning normally, we throw, using the built in exception chain
        // code blocks have. This indicates something unexpected breaking the sequence of cour code
        throw new Error("Something was wrong");
    }
    return "some normal result"; // here we indicate a function terminated normally,
                                 // and with which value.
}

节点中的回调使用我发现丑陋的(err,result...)约定。有了承诺,您可以再次获得顺序代码中错误处理的好处:

somePromise.then(function(){
     if(rejectRightAway){
          throw new Error("rejecting right away...");
     }
     return "some normal result";
})....

看看它与顺序代码的相似程度如何?每当你想要从当时的处理程序中拒绝你throw时,只要你想要实现,你就从当时的处理程序返回。就像"同步"代码。

所以在你的情况下:

//want to throw promise1 rejection //ok to just return new Error?
return new Error('promise1 rejected so through promise0 rej then mainPromise rej'); 

完全按照您的直觉想法解决:

 throw new Error("promise1 rejected so through promise0 rej then mainPromise rej");

答案 1 :(得分:2)

您需要拒绝具体承诺:

return promise0.reject(new Error('intending to throw mainPromise rejection'));