承诺拒绝功能

Noi*_*art 3 javascript firefox firefox-addon promise

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

所以我包括了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)
    }
);
Run Code Online (Sandbox Code Playgroud)

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

Ben*_*aum 5

TL:博士

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

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

Promise不仅仅是一个<buzzwords>组成顺序monadic DSL </ buzzwords>抽象而不是并发 - 它们也是安全的!

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

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.
}
Run Code Online (Sandbox Code Playgroud)

节点中的回调使用(err,result...)我觉得难看的约定.通过promises,您可以再次获得顺序代码中错误处理的好处:

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

看看它与顺序代码有多相似?每当你想要throw从当时的处理程序中拒绝你时,无论何时你想要实现,你都要从当时的处理程序返回.就像在"同步"代码中一样.

所以在你的情况下:

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

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

 throw new Error("promise1 rejected so through promise0 rej then mainPromise rej");
Run Code Online (Sandbox Code Playgroud)