1

I have encountered the following problem and I am unable to proceed. I tried some of the solutions that were posted in different question, but couldn't get it to work

In my controller, I have a $scope.init() function. I have a for loop in it which calls a function to make http.get calls to different urls, each url depends on the previous call's data, so I need it to be synchronous

$scope.init = function() {
    decodedURL = $routeParams.url;

    //evaluate some variables, ampIndex is > -1 here

    for( var i=0; ampIndex > -1; ++i)
    {
        decodedURL = decodedURL.substring(ampIndex+1, decodedURL.length);
        ampIndex = decodedURL.indexOf("&");

        $scope.getNextList(i);
        /* above function call makes the http.get call to the currentURL based on
           decodedURL, and the data is stored in variable[i+1], so for the next
           iteration, the calls should be synchronous
        */

        $q.all(asyncCall).then(function (data) {var j;} );
        /* I wrote the above dummy statement so that it is executed only after
           http.get in $scope.getNextList() function is successful, but it is
           not working 
        */
    }
};


$scope.getNextList = function(index) {

    // $currentURL is calculated
    var hello = _helpers.server.http($http, $scope.currentURL) {
        .success( function(response) {
        })
        .error( fucntion(errResponse) {
        });
    asyncCall.push(hello);
};

How do I solve this problem?

6
  • so the issue is that the call is not synchronous ? Maybe due to $q.all is inside the for loop... Commented Jul 9, 2015 at 5:36
  • ya, the call is not synchronous.....I kept $q inside because in each iteration, there is a http call and that should be synchronous Commented Jul 9, 2015 at 5:40
  • Why you using $q.all if your asyncCall only contains 1 promise ? Or might be you should refresh your asyncCall to contains 1 promise only ... Commented Jul 9, 2015 at 5:44
  • in each iteration, a promise is pushed into asyncCall, is it not right? Commented Jul 9, 2015 at 5:46
  • 1
    You are looking for sequential execution, not for synchronous one. Commented Jul 9, 2015 at 6:43

1 Answer 1

1

How about something along these lines?

http://plnkr.co/edit/pjWbNX1lnE2HtaNs1nEX?p=preview

$scope.init = function ( ){
  for (var i=0; i < 10; i++) {
    $scope.getNextList(i) // push calls into array
  };

  var index = 0;
  function makeCall() {
    $scope.asyncCall[index]
    .success(function(data) {
      if (index < $scope.asyncCall.length - 1) {
        console.log(index);
        index += 1;
        makeCall();
      }
      else {
        console.log(index); // last call
      }
    })      
  }

  makeCall();
};
Sign up to request clarification or add additional context in comments.

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.