0

I have a function where I make multiple $http.get calls. I want the function to wait until all the calls are complete. Below is the scenario

myApp.run(['$httpBackend','$http',function($httpBackend, $http){
    $http.get('data1.json').then(function(data){
        //do something
    });
    $http.get('data2.json').then(function(data){
        //do something
    });
    $http.get('data3.json').then(function(data){
        //do something
    });

    //return the function only after all three .then functions are compelte
}]);

I am using promise, however I am not sure I am using it in a right way. Right now the function .run() returns even before the $http.get().then() calls are complete.

2 Answers 2

2

You can use $q and chain them all:

myApp.run(['$httpBackend','$http', '$q', function($httpBackend, $http, $q) {
    $q.all([
        (function() {
            var d = q.defer();
            $http.get('data1.json').then(function(data){
                d.resolve(data);
            });
            return d.promise;
        })(),
        (function() {
            var d = q.defer();
            $http.get('data2.json').then(function(data){
                d.resolve(data);
            });
            return d.promise;
        })(),
        (function() {
            var d = q.defer();
            $http.get('data3.json').then(function(data){
                d.resolve(data);
            });
            return d.promise;
        })()
    ]).then(function(responses) {
        console.log(responses); //array of your responses
    });
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your quick response. I want to return the function only after all the http calls are complete. Should I add one more promise to main function like below ` maind = $q.defer();` ` $q.all([` ` //your code` ` ]).then(function(responses) {` ` maind.resolve(responses);` ` });` ` return maind;`
You dont return from async functions, you would use a callback
I dont think this resolves my issue. In the above code, the "console.log(responses)" runs after all the http calls are made. However, the entire function does not wait until all the http calls are complete. My issue is, the JSON data I load in this function is used in other services. Those services run even before all the http calls in this function gets executed. So I want to make sure all the http calls are complete before I exit this function. Is there any way to achieve this?
0

I was able to achieve my goal by injecting a factory as dependency to the run method.

Factory method where all the json files are retrieved:

myApp.factory('getJsons', ['$resource', function ($resource) {
    var data1 = $resource('data1.json').get();
    var data2 = $resource('data2.json').get();
    var data3 = $resource('data3.json').get();
    return {
        data1 : data1,
        data2 : data2,
        data3: data3
    };
}]); 

The modified run method

myApp.run(['$httpBackend','$http', 'getJsons', 
    function($httpBackend, $http, getJsons){
    //do something with getJsons.data1

    //do something with getJsons.data2

    //do something with getJsons.data3

    //The function is returned  only after all three jsons are processed
}]);

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.