好的,假设我有一个包含n 个XHR 请求的数组:
var promises = [];
var AllResults = [];
$scope.requests = [
{
"uri":"http://foo/bar/action1",
"name":"Action1"
},{
"uri":"http://bar/foo/action2",
"name":"Action2"
},{...},{...},{...}
];
var ParseMyResultsArray = function(){
console.log(AllResults);
//...
};
angular.forEach($scope.requests, function(request){
var promise = $http.get(request.uri);
promises.push(promise);
});
angular.forEach(promises, function(promise){
promise.then(function(results){
AllResults.push(results);
});
});
//Then, when all promise are resolved...
ParseMyResultsArray();
ParseMyResultsArray()
在不知道requests
数组大小的情况下,如何确保在所有承诺都解决时调用?
谢谢你的帮助!