Click here to Skip to main content
15,113,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a list of links that I want to query and return the result to one large array object

Currently my code as shown below just overwrites itself whenever it runs

Thanks

What I have tried:

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

}
Posted
Updated 1-Oct-21 1:10am
v2

1 solution

You'll want to make use of promises and arrays quite heavily to achieve what you want. Since promises are non-blocking (and therefore won't block the for loop) you'll need to collect your promises and wait for them all to finish executing before you can access all of the results. You can use the Promise.allSettled method to do that.

To append values to an array you just need to create an array and use the push method:

JavaScript
let values = [];
value.push(..);

I've put together a JSFiddle which shows how to use Promise.allSettled[^] which simply uses a random delay in a method call to simulate the HTTP request. All you'd need to do is retrofit what I've provided into your own code.

Worth noting that I'm not sure exactly what happens when an exception occurs (ie when a catch block would be required), might be worth trying it out and maybe doing some further research.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900