0

In my application I am using AngularJS and nodeJS for rest api and its working fine. But when I try to call api using for loop its having some problems. Actually only last set of Parameters are passing to the api.

It means if I pass different parameters using loop 3 times then only last set of parameters passing to the api 3 times. First and Second are not send to api. Can anybody have any Idea??

Here is a sample code I use in Angularjs Factory.

var firstTimeData = {
                        "firstid": serializedJsonData.firstid,
                        "secondid": serializedJsonData.secondid,
                        "otherid": serializedJsonData.otherid

                    }
                    for (var i = 0; i < data[0].length; i++) {
                        for (var j = 0; j <= 2; j++) {
                            firstTimeData.dshdetailid = data[i][j].dshdetailid;
                            if ((data[i][j].defaultTitle).trim() == 'Scorecard Graph') {
                                BlankApi.getData(firstTimeData, 'slide' + j, 'periodname', 'amount');
                            }
                        }
                    }
code Zero
  • 115
  • 2
  • 8
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Rahul Nanwani May 30 '16 at 06:04
  • Refer to this http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example?rq=1 for a detailed explanation about your question – Rahul Nanwani May 30 '16 at 06:05

1 Answers1

0

To fix this issue you have to make asynchronous calls in loop,

Here in this code for loop get continuously executed without waiting for each api call to get finished.

Refer this post to get idea about how to use asynchronous calls in loop

Community
  • 1
  • 1