versb
versb

Reputation: 101

$http get loop append response data together

I have a list of links, with a number in each one from 1 up to however many there are. My goal is to write a for loop, that goes through each link, runs a HTTP get, then saves the response data to an array object. I have tried various methods, but its difficult because of the fact that I am dealing with $scope variables, so none of the methods I find work.

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 = ??  

}

Upvotes: 3

Views: 183

Answers (1)

SaminatorM
SaminatorM

Reputation: 630

Try this way;

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
});

Upvotes: 1

Related Questions