1

I want to build an array of objects but for each object I need multiple chained http calls. Eg:

var objects = [];
$http.get(/getDataUrl)
.then(function(data){
   for(i = 0; i < data.length; i++)
   {
       objects[i] = { value1 : data.value }
   }
   for(i = 0; i < objects.length; i++)
   {
       http.get(objects[i].value1)
       .then(function(data){
           objects[i].value2 = data.value;
       }
   }
})

Any ideas how to properly execute this mess?

3
  • 1
    Your solution is not working ? with the very few info that you provided, I assume that it shall work Commented Jun 27, 2015 at 22:39
  • I don't have access to current counter value 'i' in the chained call. Commented Jun 27, 2015 at 23:01
  • i think you are getting this situation - always i is pointing to the last index? Commented Jun 28, 2015 at 4:58

3 Answers 3

2

You will always get 'i' value to be the final index because the renderer will print it when the ajax request gives 200, this will take some time. And in that short time, the for loop would have been executed, therefore you will always get the last index for the value of 'i'

To solve this, you will have to use, closure inside loops

Modify your code like as follows,

var objects = [];
$http.get(/getDataUrl)
.success(function(data){
   for(i = 0; i < data.length; i++)
   {
       objects[i] = { value1 : data.value }
   }
   for(i = 0; i < objects.length; i++)
   {
       (function(index) {
           var currentURL = objects[i].value1;

           $http.get(currentURL)
           .success(function(data) {

               // both currentURL and i value can be accessed here 
               console.log(currentURL);
               console.log(index); // i value  = index

               objects[index].value2 = data.value;
           });
       })(i);
   }
})

Now you have access of index inside the anonymous function. Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

You can try to use success instead of then, as $http isn't a standard promise :

var objects = [];
$http.get('/getDataUrl')
.success(function(data){
   for(i = 0; i < data.length; i++)
   {
       objects[i] = { value1 : data.value }
   }
   for(i = 0; i < objects.length; i++)
   {
       http.get(objects[i].value1)
       .success(function(data2){
           objects[i].value2 = data2.value;
       }
   }
})

Comments

0

Try something like that : Only one loop to rules them all ;)

$http.get(/getDataUrl)
.then(function(data){
   for(i = 0; i < data.length; i++)
   {
       objects[i] = { value1 : data.value }


       $http.get(objects[i].value1)
       .then(function(data){
           objects[i].value2 = data.value;
       }
    }

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.