1

I am making a request to URLs in my forEach loop. I want the calls to be made synchronously...

angular.forEach(param, function(value) {
    FactoryWithURLCallingFunction.URLCallingFunction(param1, value, param3).success(function(data){
        console.log("Url returns: " + data);
        });
    });

Any help on this?

3
  • Why do you want them synchronously? You may be better off nesting the promises ? Commented Nov 12, 2015 at 11:17
  • Use the $q.all method docs.angularjs.org/api/ng/service/$q Commented Nov 12, 2015 at 11:17
  • @christiandev - because I require the first request to the url to complete and return before I can start the next one. Commented Nov 13, 2015 at 14:43

1 Answer 1

3

You should not make synchronous AJAX requests (although, it's possible), it's a sure way to making irresponsive UI. Instead, use proper promises capabilities:

$q.all(param.map(function(value) {
    return FactoryWithURLCallingFunction.URLCallingFunction(param1, value, param3).then(function (response) {
        console.log('Url returns:', response.data);
        return response.data;
    });
})).then(function(data) {
    console.log('All loaded:', data);
});
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.