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

时间:2021-03-08 19:42:00

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

仅考虑Serviceforpromise1失败的情况。 $ q.reject被发送回主链,所以我正在等待错误回调“promise1 .then(”被调用并且它按预期工作。我决定将该错误转移到“promise2 .then” “所以在这个错误回调中我添加了行返回$ q.reject();但它从未到达第二个错误回调(”promise2 .then“一个)并且我不明白为什么(如Serviceforpromise1,我返回了拒绝诺言 !)

I will be happy to deeply understand what is happening here. Thanks for your help.

我很乐意深入了解这里发生的事情。谢谢你的帮助。

1 个解决方案

#1


7  

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

如果从then()中的成功或错误处理程序返回被拒绝的promise,则then()返回的promise将解析为被拒绝的promise。注意:

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

这里需要注意的一点是,在最后一个处理程序中,您将返回错误变量,而不是抛出异常或返回被拒绝的承诺。因此,在这种情况下,promiseend将使用该错误变量的值成功解析。

#1


7  

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

如果从then()中的成功或错误处理程序返回被拒绝的promise,则then()返回的promise将解析为被拒绝的promise。注意:

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

这里需要注意的一点是,在最后一个处理程序中,您将返回错误变量,而不是抛出异常或返回被拒绝的承诺。因此,在这种情况下,promiseend将使用该错误变量的值成功解析。