简体   繁体   English

AngularJS处理$ q.all中的资源被拒绝

[英]AngularJS handling rejected resources in $q.all

I'm trying to handle errors with my resources, and then handle rejection of resources in my $q.all() . 我正在尝试使用我的资源处理错误,然后处理我的$q.all()的资源拒绝。

This is my code: 这是我的代码:

var user = User.get({id: 1}, function() {
    // Success
}, function(response) {
    // Error
    return $q.reject(response);
});

var promiseList = [user];
$q.all(promiseList).then(function(){
  // Success <-- this seems to run all the time
  }, function(response) {
    // Error <-- this never seems to run but I want it to
});

When my User resource receives a 404, the error callback handles it and returns a $q.reject . 当我的用户资源收到404时,错误回调会处理它并返回$q.reject

However the success callback in my $q.all gets called, not my error callback. 但是我的$q.all成功回调被调用,而不是我的错误回调。 I would've thought because I am rejecting my promise the $q.all error callback would be fired? 我会想,因为我拒绝承诺$q.all错误回调将被解雇?

I appreciate I only have 1 item in my promiseList but that shouldn't make a difference should it? 我很欣赏我的promiseList只有1个项目但是应该没有区别吗?

According to Michael in the comments 根据迈克尔的评论

I changed 我变了

var promiseList = [user];

To this: 对此:

var promiseList = [user.$promise];

And now the $q.reject() is being picked up by the $q.all() . 现在$q.reject()$q.all()

Awesome, thanks for the advice. 太棒了,谢谢你的建议。

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

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