【发布时间】:2016-09-05 09:23:37
【问题描述】:
我有这个角度的代码,
$http({
method:'POST',
url :'someURL', //returns an array of urls [url1, url2, url3..]
data : dataObj
})
.then(function(response) {
var items = response.data;
var promises = [];
$scope.output =[];
items.forEach(function(el){
return promises.push($http.get(el)); //fills the promise[] array
});
var ignore = function(x) { return x.catch(function(){}); } // To ignore if promise does not get resolved (only accept responses with status 200)
var all = $q.all( promises.map(ignore) ); //chaining promises array
all.then(function success(d){
console.log($scope.output); //want the output to be ["text1", "text2", "text3"...]
});
for (var i=0; i < promises.length ; i++){
promises[i].then(success).catch(function (){
});
function success(r){
$scope.output.push(r.data.text); //{text: "text1"}
}
}
});
此操作的结果存储在$scope.output 中。在执行时,我得到的输出为["text2", "text3", "text1" ...],这不是串行的。我的问题是如何以串行方式执行此操作,以便输出为["text1", "text2", "text3" ...]
【问题讨论】: