Angular $q returning promise multiple $http calls

后端 未结 3 2100
梦毁少年i
梦毁少年i 2021-01-05 07:16

I\'m working on an $http call that loops over each of multiple api\'s and returns all of the data in one object. I usually have the promise ready to resolve when the $http c

相关标签:
3条回答
  • 2021-01-05 07:52

    You should add a list of promises in $q ( not resolved promises like in your code ) , which is a $promise service

    Example:

    var firstPromise = service1.getMethod1().$promise;
    var secondPromise = service2.getMethod2().$promise;
    $q.all([firstPromise, secondPromise]).then(function(dataList){
         // dataList[0] will be result of `firstPromise`
         // dataList[1] will be result of `secondPromise`
    });
    
    0 讨论(0)
  • 2021-01-05 07:58

    $q.all and a map function are what you need here:

    function getSearchData() {
        return {
            // returns a promise for an object like:
            // { abo: resultFromAbo, ser: resultFromSer, ... }
            loadDataFromUrls: function () {
                var apiList = ["abo", "ser", "vol", "con", "giv", "blo", "par"];
    
                return $q.all(apiList.map(function (item) {
                    return $http({
                        method: 'GET',
                        url: '/api/' + item
                    });
                }))
                .then(function (results) {
                    var resultObj = {};
                    results.forEach(function (val, i) {
                        resultObj[apiList[i]] = val.data;
                    });
                    return resultObj;        
                });
            }
        };
    }
    
    0 讨论(0)
  • 2021-01-05 08:08

    If you have an arbitrary set of api calls I would do something like this:

    function getSearchData(){
        var deferred = $q.defer();
        var noOfCalls = apiList.length;
        var results = [];
        var called = 0;
    
        angular.forEach(apiList, function(item, key) {
            $http.get(url).then(function(result){
               results.push(result);
               called++;
               if(called == noOfCalls){
                  deferred.resolve(results);
               }     
            })
       });
    
        return deferred.promise;
    }
    

    However if you know what each api call represents its better to use $.all in this way

    function search1(){
          return $http.get(search1Url).then(function(result){
              // do something to it
              return result; 
          });
    }
    
    function search2(){
          return $http.get(search2Url).then(function(result){
              // do something to it
              return result; 
          });
    }
    
    function search3(){
          return $http.get(search3Url).then(function(result){
              // do something to it
              return result; 
          });
    }
    
    function search4(){
          return $http.get(search4Url).then(function(result){
              // do something to it
              return result; 
          });
    }
    
    function getSearchResult(){
    
        return $q.all([search1(), search2(), search3(), search4()]).then(function(results){
           // OPTIONAL  aggregate results before resolving
           return results;
        });
    }
    
    0 讨论(0)
提交回复
热议问题