简体   繁体   English

$ q.reject和AngularJS链接承诺中的处理错误

[英]$q.reject and handling errors in AngularJS chained promises

I'm having trouble understanding a basic concept of error handling with chaining promises. 我无法理解链接承诺的错误处理的基本概念。 In order to learn the rules, I have written a simple example, guessing what the result will be. 为了学习规则,我写了一个简单的例子,猜测结果是什么。 But unfortunatly it doesn't behave as I though it will. 但不幸的是,它不会像我一样表现。 I have read multiple articles about the subject but perhaps can't I get details because of my poor english language. 我已经阅读了很多关于这个主题的文章,但由于我的英语不好,我可能无法得到细节。

Anyway, here is my code : 无论如何,这是我的代码:

    var promiseStart = $q.when("start");
    var promise1 = promiseStart.then(function() {
            return Serviceforpromise1.get();
    });
    var promise2 = promise1.then(function(data1)
    {
            return Serviceforpromise2.get(data1);
    },function(error)
    {
            return $q.reject();
    });
    var promiseend = promise2.then(function(data2)
    {
            return data2;
    },function(error)
    {
            return error;
    });
    return promiseend;

Well I know that it can be way better coded but it's just for the purpose. 嗯,我知道它可以更好地编码,但它只是为了这个目的。 Here is the code of Serviceforpromise1 function : 以下是Serviceforpromise1函数的代码:

    function Serviceforpromise1()
    {
            ...
            return $http.get(*whatever*).then(function (data){
                return data;
            },function(error)
            {
                return $q.reject();
            });
    }

Consider only the case of Serviceforpromise1's failure. 仅考虑Serviceforpromise1失败的情况。 A $q.rejec t is sent back to main chain so I'm waiting the error callback of " promise1 .then( " to be called and it worked as expected. I decided for the example to transfert the error to the " promise2 .then " so in this error callback I added the line return $q.reject() ; But it never reached the second error callback (the " promise2 .then " one) and I don't understand why (like Serviceforpromise1, I returned a rejected promise !) $ q.rejec t被发送回主链,所以我正在等待“ promise1 .then( ”被调用的错误回调,并且它按预期工作。我决定将该错误转移到“ promise2”。然后 “所以在这个错误回调我添加了行返回$ q.reject() ;但它从未达到第二个错误回调(” promise2 .then “一个)我不明白为什么(像Serviceforpromise1,我返回了一个拒绝承诺!)

I will be happy to deeply understand what is happening here. 我很乐意深入了解这里发生的事情。 Thanks for your help. 谢谢你的帮助。

Your understanding is correct, and the problem appears to lie somewhere in the way you are trying to observe this behavior (in something you haven't shown us). 你的理解是正确的,问题似乎在于你试图观察这种行为的某种方式(在你没有向我们展示过的东西中)。

If you return a rejected promise from either a success or error handler in then() , then the promise returned by then() will resolve to a rejected promise. 如果返回从成功错误处理程序被拒绝的承诺, then()然后通过返回的承诺then()将解析为一个拒绝承诺。 Observe: 注意:

 angular.module('app', []) .controller('C', [ '$q', function ($q) { var promiseStart = $q.when("start"); var promise1 = promiseStart.then(function (value) { console.log('Got a value:', value); return $q.reject('Error!'); }); var promise2 = promise1.then(function (data1) { return "Got some stuff"; }, function (error) { console.log("Caught an error:", error); return $q.reject('New error'); }); var promiseend = promise2.then(function (data2) { return data2; }, function (error) { console.log('Caught an error:', error); // <-- this is logged to the console return error; }); return promiseend; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script> <div ng-app='app' ng-controller='C'></div> 

One thing to note here is that in that last handler, you are returning the error variable, and not throwing an exception or returning a rejected promise. 这里需要注意的一点是,在最后一个处理程序中,您将返回 error变量,而不是抛出异常或返回被拒绝的承诺。 So in this case, promiseend will successfully resolve with the value of that error variable. 因此,在这种情况下, promiseend将使用该error变量的值成功解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM