TheNone
TheNone

Reputation: 5802

Fetching Data From Two Or More Api Synchronously

I'm using angularjs in my project:

var getApi = function(){
     $http.get(link)
    .then(function(response) {$scope.data = response.data.api});   
}

But this time, I have to fetch link from two or more links. How can I do this? I have to fetch data from 5 api Synchronously and get sum of data.

Upvotes: 1

Views: 48

Answers (1)

Martijn Welker
Martijn Welker

Reputation: 5605

You can do something like this:

var getApi = function(){
    $q.all([
         $http.get(link),
         $http.get(link),
         $http.get(link),
         $http.get(link),
         $http.get(link)
    ]).then(function(resultArray) {
        // resultArray will now contain 5 objects with responses  
    });
}

More info about $q.all here.

Upvotes: 4

Related Questions