I am executing multiple $http
calls in parallel (async). My code logic looks something like this -
我正在并行执行多个$ http调用(异步)。我的代码逻辑看起来像这样 -
$scope.ids = [1, 2, 3, 4];
var promises = [];
for(var i in $scope.ids){
promises.push(
$http.get('http://0.0.0.0/t2/' + $scope.ids[i])
);
}
$q.all(promises).then(
function(res){
console.log(res);
},
function(res){
console.log(res);
}
);
Now the $q.all()
docs say that
现在$ q.all()文档说明了这一点
Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.
返回将使用值的数组/散列解析的单个promise,每个值对应于promises数组/散列中相同索引/键的promise。如果任何承诺通过拒绝得到解决,则此结果承诺将被拒绝并具有相同的拒绝值。
Now when all the promises pass, I get an expected response of an array containing 4 elements.
现在当所有的promise都通过时,我得到一个包含4个元素的数组的预期响应。
Success -> Object[4]
成功 - >对象[4]
However, in the case where all fail, the res
output shows only the one which failed.
但是,在全部失败的情况下,res输出仅显示失败的输出。
Failure -> Hash
失败 - >哈希
This is as expected and what the Angular Docs mention.
这是预期的,以及Angular Docs提到的内容。
In my case I want to know the status of all the http request I made, even if it was a failure I require the result. Certainly $q.all() will break out the moment one of the promises fails. I want to wait till all the promises have either passed or failed and then get the results. How do I do this?
在我的情况下,我想知道我所做的所有http请求的状态,即使它是一个失败我需要结果。肯定$ q.all()会在其中一个承诺失败的那一刻爆发。我想等到所有承诺都通过或失败然后得到结果。我该怎么做呢?
1 个解决方案
#1
2
An idea is to utilize the error callback of the promise to return a meaningful status:
一个想法是利用promise的错误回调来返回有意义的状态:
function Failure(r) {
this.response = r;
}
$scope.ids = [1, 2, 3, 4];
var promises = [];
for(var i in $scope.ids){
promises.push(
$http.get('http://0.0.0.0/t2/' + $scope.ids[i]).then(
function(r) {
return r;
},
function failure(r) {
// this will resolve the promise successfully (so that $q.all
// can continue), wrapping the response in an object that signals
// the fact that there was an error
return new Failure(r);
}
);
);
}
Now $q.all()
will always succeed, returning the array of results. Check each result with result[i] instanceof Failure
. If this condition is true, there was a failure in that request - get the response doing result[i].response
. IT may need some tweaking, but hopefully you get the point.
现在$ q.all()将始终成功,返回结果数组。使用result [i] instanceof Failure检查每个结果。如果这个条件为真,则该请求失败 - 得到响应结果[i] .response。 IT可能需要一些调整,但希望你明白这一点。
#1
2
An idea is to utilize the error callback of the promise to return a meaningful status:
一个想法是利用promise的错误回调来返回有意义的状态:
function Failure(r) {
this.response = r;
}
$scope.ids = [1, 2, 3, 4];
var promises = [];
for(var i in $scope.ids){
promises.push(
$http.get('http://0.0.0.0/t2/' + $scope.ids[i]).then(
function(r) {
return r;
},
function failure(r) {
// this will resolve the promise successfully (so that $q.all
// can continue), wrapping the response in an object that signals
// the fact that there was an error
return new Failure(r);
}
);
);
}
Now $q.all()
will always succeed, returning the array of results. Check each result with result[i] instanceof Failure
. If this condition is true, there was a failure in that request - get the response doing result[i].response
. IT may need some tweaking, but hopefully you get the point.
现在$ q.all()将始终成功,返回结果数组。使用result [i] instanceof Failure检查每个结果。如果这个条件为真,则该请求失败 - 得到响应结果[i] .response。 IT可能需要一些调整,但希望你明白这一点。