我有一个链接列表,每个链接都有一个数字,从 1 到有多少个。我的目标是编写一个 for 循环,遍历每个链接,运行 HTTP get,然后将响应数据保存到数组对象。我尝试了各种方法,但由于我正在处理 $scope 变量,所以很难,所以我找到的方法都不起作用。
for (let i = 0; i < 1000; i++) {
$http.get("Start of URL"
+ i + "End of URL")
.then(function (response){
$scope.datasingle = response.data;
});
$scope.datacombined = ??
}
最佳答案
试试这个方法;
const promisses = [];
for (let i = 0; i < 1000; i++) {
promisses.push( new Promise(function (resolve, reject) {
$http.get("Start of URL"+ i + "End of URL").then(function(response){
resolve(response.Data);
});
}));
}
Promise.all(promisses).then(function (values) {
console.log(values); //you will get all the resolved result in this array and do what you want
});
关于javascript - $http get 循环将响应数据追加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69400883/