简体   繁体   中英

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

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 .

However the success callback in my $q.all gets called, not my error callback. I would've thought because I am rejecting my promise the $q.all error callback would be fired?

I appreciate I only have 1 item in my promiseList but that shouldn't make a difference should it?

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

Awesome, thanks for the advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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