$ q.reject和AngularJS链式诺言中的处理错误 [英] $q.reject and handling errors in AngularJS chained promises

查看:57
本文介绍了$ q.reject和AngularJS链式诺言中的处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解带有链接承诺的错误处理的基本概念. 为了学习规则,我写了一个简单的示例,猜测结果是什么.但不幸的是,它的行为不尽如人意. 我已经阅读了多篇有关该主题的文章,但由于我的英语不好,也许我无法获得详细信息.

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;

我知道它可以被更好地编码,但这只是出于目的. 这是Serviceforpromise1函数的代码:

Well I know that it can be way better coded but it's just for the purpose. Here is the code of Serviceforpromise1 function :

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

仅考虑Serviceforpromise1失败的情况. $ q.rejec t被发送回主链,因此我等待" promise1 .then(""的错误回调被调用,并且按预期方式工作.我决定为该示例将错误转移到" promise2 .then ",因此在此错误回调中,我在行中添加了 return $ q.reject();. 但是它从未到达第二个错误回调(" promise2 .then "一个),而且我也不明白为什么(像Serviceforpromise1一样,我返回了被拒绝的承诺!)

Consider only the case of Serviceforpromise1's failure. A $q.reject 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 !)

我将很高兴深入了解这里发生的事情. 感谢您的帮助.

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

如果从 成功或错误处理程序中的 返回了被拒绝的承诺,则then()返回的承诺将解析为被拒绝的承诺.观察:

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

这里要注意的一件事是,在最后一个处理程序中,您正在返回 error变量,并且不会引发异常或返回被拒绝的promise.因此,在这种情况下,promiseend将成功 使用该error变量的值进行解析.

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. So in this case, promiseend will successfully resolve with the value of that error variable.

这篇关于$ q.reject和AngularJS链式诺言中的处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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