javascript $q.reject 和处理 AngularJS 链式承诺中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29175455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$q.reject and handling errors in AngularJS chained promises
提问by Patrice Pistone
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”。然后“所以在这个错误回调中我添加了行return $q.reject();但它从未到达第二个错误回调(“ promise2 .then”一个),我不明白为什么(像 Serviceforpromise1,我返回了一个拒绝承诺!)
I will be happy to deeply understand what is happening here. Thanks for your help.
我很高兴能够深入了解这里发生的事情。谢谢你的帮助。
回答by JLRishe
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 eithera success or error handler in then()
, then the promise returned by then()
will resolve to a rejected promise. Observe:
如果您从中的成功或错误处理程序then()
返回被then()
拒绝的承诺,则 返回的承诺将解析为被拒绝的承诺。观察:
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 returningthe error
variable, and not throwing an exception or returning a rejected promise. So in this case, promiseend
will successfullyresolve with the value of that error
variable.
这里有一点要注意的是,在这最后的处理程序,你回来的error
变量,而不是抛出异常或返回被拒绝的承诺。所以在这种情况下,promiseend
将成功解析该error
变量的值。