承诺拒绝功能 [英] Promise rejecting in function

查看:162
本文介绍了承诺拒绝功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我怎么能拒绝承诺?只要返回新的错误('blah')不能正常工作,错误控制台抛出.then不是一个函数或类似的东西。



所以我包含Promise.jsm然后做了返回Promise.reject(new Error('blah'))所以我的问题是:if im in a诺言如:那么我不必要返回Promise.rject,并可以只返回新的错误权?

  // mainPromise链to promise0并链接promise1 
函数doPromise(rejectRightAway){
如果(rejectRightAway){
返回Promise.reject(新错误('打算抛出mainPromise拒绝')); //是否使用Promise.reject是正确的地方? //返回错误控制台中的新错误,例如:'.then不是函数',并指向mainPromise的onReject函数,然后
}

promise0.then(
函数(){

var promise1 = somePromiseObject;
promise1.then(
function(){
alert('promise1 success');

$ alert('promise1 rejected'+ aRejReason.message);
return new Error('promise1 reject so all promise0 rej then mainPromise rej');
函数(aRejReason){
alert //想抛出promise1拒绝//正确返回新的错误?



$ a
alert('promise0被理由='+ aRejReason.message);
}
)拒绝;

return promise0;

}

var mainPromise = doPromise();
mainPromise.then(
function(){
alert('mainPromise success');
},
function(aRejectReason){
alert(' mainPromise被拒绝,原因='+ aRejectReason.message)
}
);

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

解决方案

tl:dr


  • 为了表示一个操作的正常完成,返回结果返回结果

  • 为了表示一个异常, c $ c>抛出new Error(reason)就像在同步代码中一样,promises是安全的。
  • 可以显式地从链中返回拒绝,但是往往不需要。






有关promise的一个很棒的事情是他们修复异步代码中的异常处理。

承诺不仅仅是一个<流行语>构成顺序单点DSL< /流行语>并发的抽象 - 它们也是安全的!



在这方面,抛出安全性真的很棒,你可以像在连续代码中一样行事:

 if(somethingWrong){
//而不是正常返回,我们抛出,使用内置的异常链
//代码块有。这意味着一些意想不到的事情破坏了代码的顺序
throw new Error(Something was wrong);
}
返回一些正常结果; //这里我们指出一个正常终止的函数,
//以及哪个值。





节点中的回调使用(err,result。 ..)惯例,我觉得丑陋。有了promise,你可以再次获得错误处理的好处:

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

看看它和顺序代码有多相似?每当你想从你的处理程序中抛弃> throw 时,只要你想完成你从那个处理程序返回。就像在同步代码中一样。

所以在你的情况下:

  //想抛出promise1拒绝//正确返回新的错误? 
返回新的错误('promise1拒绝所以通过promise0 rej然后mainPromise rej');

完全可以用你直觉上想要做的事来解决:

 抛出新的错误(promise1拒绝,所以通过promise0 rej然后mainPromise rej); 


In javascript how can I reject a promise? Simply returning new Error('blah') doesnt work right error console throws ".then is not a function" or something like that.

So I included Promise.jsm and then did return Promise.reject(new Error('blah')) so my question is: if im in a promise like: then i dont have to return Promise.rject and can just return new Error right?

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

im not sure if this code works, so my question is how do i reject --- like when is it right to return Promise.reject(new Error()) and when to just new Error()?

解决方案

tl:dr

  • In order to indicate normal completion of an operation, you return its result return result.
  • In order to indicate an exception, you throw new Error(reason) just like in synchronous code, promises are throw safe.
  • It is possible to explicitly return rejections from chains, but often unneeded.

One of the great things about promises is that they fix exception handling in asynchronous code.

Promises are not just a < buzzwords> composing sequential monadic DSL < /buzzwords> abstraction over concurrency - they are also throw safe!

Throw safety is really awesome in this regard, you can act just like ou would in sequential code:

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.
}

Callbacks in node use the (err,result...) convention which I find ugly. With promises, You gain the benefits of error handling you have in sequential code again:

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

See how similar it is to sequential code? Whenever you want to reject you throw from the then handler, whenever you want to fulfill you return from the then handler. Just like in "synchronous" code.

So in your case:

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

Is solved exactly with what you intuitively wanted to do:

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

这篇关于承诺拒绝功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆