如何使用$ q promises在角度js中并行处理多个(in循环)异步api调用?

时间:2023-01-06 19:40:06

I have an api :"http://me.apiary.com/1/user/{uid}", which i have to call multiple times parallely and asynchronously with various uid.how is it possible to call parallely and asychronously in angular js using promise($q) ? i have following code:

我有一个api:“http://me.apiary.com/1/user/{uid}”,我必须与各种uid.how并行和异步调用多次,可以在角度中并行和异步调用js使用promise($ q)?我有以下代码:

Angular Factory:

angular.module('witnessApp.Service')
  .factory('File',['$http', function ($http) {
    var baseUrl="http://me.apiary.com";
      getFileById:function(uid){
      return  $http({
          method: 'GET',
          url: baseUrl+'/1/user/'+uid
        });
      }
    }


    return {
      getFileById:fileTools.getFileById,
    }

  }]);

Angular Controller:

'use strict';
angular.module('witnessApp.Controller')
  .controller('shareCtrl',['$scope','FileShare','$window','$routeParams','shareImgId','File', function ($scope,FileShare,$window,$routeParams,shareImgId,File) {
    var id=$window.localStorage.id;
    $scope.sharedImagesByID=[];
    var uids=[1,2,3,4,5,6,7,8,9,0,11,10,12,13,14];

    $scope.getFileById=function(){
      for(var key in uids){
        File.getFileById(uids[key])
        .success(function(data, status, headers) {
        $scope.sharedImagesByID.push(data.data)
      })
      .error(function(data, status, headers, config) {
        console.error('error in loading File');
      });


  }

}

i tried using $q.all([]). but how i can handle loop using $q.all().i dont have any idea.please suggest me how to do this? Is it possible to first 3 api call parallely.after that view should be render.and again 3 api call in background process. and so on till last api call. is it possible?

我尝试使用$ q.all([])。但我怎么能用$ q.all()来处理循环。我什么都没有。请问如何做到这一点?是否可以并行地进行3次api调用。之后应该渲染该视图。并且在后台进程中再次进行3次api调用。等到最后的api电话。可能吗?

1 个解决方案

#1


2  

Try this:

$scope.getFileById = function() {

    var promises = [];
    for(var key in uids) {
       var promise = File.getFileById(uids[key]);

       promise.success(function(data, status, headers) {
            $scope.sharedImagesByID.push(data.data);
       });

       promise.error(function(data, status, headers, config) {
            console.error('error in loading File');
       });

       promises.push(promise);
     }

     $q.all(promises);
}  

Define array, push all promises in to it, and call $q.all();

定义数组,将所有承诺推送到它,并调用$ q.all();

#1


2  

Try this:

$scope.getFileById = function() {

    var promises = [];
    for(var key in uids) {
       var promise = File.getFileById(uids[key]);

       promise.success(function(data, status, headers) {
            $scope.sharedImagesByID.push(data.data);
       });

       promise.error(function(data, status, headers, config) {
            console.error('error in loading File');
       });

       promises.push(promise);
     }

     $q.all(promises);
}  

Define array, push all promises in to it, and call $q.all();

定义数组,将所有承诺推送到它,并调用$ q.all();