简体   繁体   English

链接通过for循环创建的多个promise

[英]Chaining multiple promises created through for loop

I have been studying promises through this link and I understood the idea of it 我一直在通过这个链接研究诺言,我理解了它的想法

var parentID;

$http.get('/api/user/name')
  .then(function(response) {

  parentID = response.data['ID'];
  for (var i = 0; i < response.data['event-types'].length; i++) {
    return $http.get('/api/security/' + response.data['event-types'][i]['key']);
  }

  })
  .then(function(response) {

    // response only returns one result of the many promises from the for loop
    // do something with parentID; 

  });

However, my use case requires to loop through and send create more than 1 promises. 但是,我的用例需要遍历并发送创建多个1的Promise。 I have tried to chain an as example above but only the only one of the promise created from the for loop was executed. 我在上面尝试链接一个示例,但是仅执行了从for循环创建的一个promise。

How can I continue chaining all of the promises while continue having access to the variable parentID? 如何在继续访问变量parentID的同时继续链接所有的Promise?

You should use $q.all because it is integrated with the AngularJS digest cycle. 您应该使用$q.all因为它与AngularJS摘要循环集成在一起。

var parentID;

$http.get('/api/user/name')
  .then(function(response) {

      parentID = response.data['ID'];
      var promiseList = [];
      for (var i = 0; i < response.data['event-types'].length; i++) {
          var iPromise = $http.get('/api/security/' + response.data['event-types'][i]['key']);
          promiseList.push(iPromise);
      };
      return $q.all(promiseList);

  })
  .then(function(responseList) {

       console.log(responseList);

  });

From the Docs: 从文档中:

all(promises); 全部(应许);

Combines multiple promises into a single promise that is resolved when all of the input promises are resolved. 在将所有输入的Promise解析后,将多个Promise合并为一个Promise。

Parameters 参量

An array or hash of promises. 承诺的数组或哈希。

Returns 退货

Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. 返回将用值的数组/哈希值解析的单个promise,每个值对应于promise数组/哈希中相同索引/键处的promise。 If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value. 如果任何一个承诺都被拒绝解决,则此结果承诺将以相同的拒绝值被拒绝。

-- AngularJS $q Service API Reference -- $q.all -AngularJS $ q服务API参考-$ q.all

You can utilize Promise.all() , substitute Array.prototype.map() for for loop 您可以利用Promise.all() ,将Array.prototype.map()替换为for循环

  var parentID;

  $http.get('/api/user/name')
  .then(function(response) {    
  parentID = response.data['ID'];
  return Promise.all(response.data['event-types'].map(function(_, i) {
    return $http.get('/api/security/' + response.data['event-types'][i]['key'])
    })) 
  })
  .then(function(response) {

    // response only returns one result of the many promises from the for loop
    // do something with parentID; 

  })
  .catch(function(err) {
    console.log(err);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM