【问题标题】:Strongloop promise inside loop循环内的 Strongloop 承诺
【发布时间】:2016-03-17 08:18:48
【问题描述】:

我正在尝试在 for 循环中调用 loopback find 函数,将迭代中的值传递给 loopback 函数。代码的主要问题可以用以下方式表示:

for (var a = 0; a < $scope.countries.length; a++) {
  $scope.getEmFacPurElec($scope.countries[a], 'ton/kWh', 'CO2e').then(function(result) {
    emFacPurElecToUse = $scope.emFacPurElecs;
}

这里是被调用的函数:

$scope.getEmFacPurElec = function (country, unit, ghgType) {
   var defer = $q.defer();
   $scope.emFacPurElecs = [];

   $scope.emFacPurElecs = Country.emFacPurElecs({
      id: country.id,
      filter: {
               where: {
                       and: [
                             {unit: unit},
                             {ghgType: ghgType}
                            ]
                      }
              }
   });   

   defer.resolve('Success getEmFacPurElec');
   return defer.promise;
};             

问题在于,loopback promise 函数被调用,然后返回 undefined,这意味着它在获取要分配给 emFacPurElecToUse 的值之前移动到 for 循环的下一次迭代。在转到下一个国家/地区之前,我需要对该国家/地区的该变量进行更多计算。

我已经研究过使用 $q.all 作为一种可能的解决方案,并且还按照 http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html 使用 array.map(新手错误 #2:WTF,我如何使用 forEach() 和 promise?),但我只是无法弄清楚如何将它们组合在一起以使其工作。我应该改用 forEach 吗?

我还看到了这个链接angular $q, How to chain multiple promises within and after a for-loop(以及其他类似的链接),但我没有需要在 for 循环内处理的多个承诺。我需要检索该国家/地区的一个 emFacPurElecs 的值,对其进行一些处理,然后移至下一个国家/地区。我觉得我很接近,但我无法理解如何编写这个特定的功能。非常感谢任何帮助。

【问题讨论】:

标签: angularjs promise angular-promise loopbackjs strongloop


【解决方案1】:

在我看来,您确实在 for 循环中处理了多个承诺,正如您所说的“在移动到下一个国家/地区之前,我需要对该国家/地区的该变量进行更多计算。”这一切都应该在我建议的承诺链中完成 - calcEmFacPurElec

$scope.calcEmFacPurElec = function (country, unit, ghgType) {
   $scope.getEmFacPurElec(country, unit, ghgType).then(function(countryEmFacPurElecs) {
    // do something with countryEmFacPurElecs

    return countryEmFacPurElecs;
}

$scope.getEmFacPurElec = function (country, unit, ghgType) {
   var defer = $q.defer();

   defer.resolve(Country.emFacPurElecs({
      id: country.id,
      filter: {
               where: {
                       and: [
                             {unit: unit},
                             {ghgType: ghgType}
                            ]
                      }
              }
   });   );
   return defer.promise;
};      

希望以上是指向正确方向的指针!

当您想要对一组项目执行承诺链时,正如您所确定的,Promise.all(使用您需要的任何承诺实现)就是您想要的。 .all 接受一个 Promises 数组,所以在你的 for 循环中你可以这样做:

var promises = []; 
for (var a = 0; a < $scope.countries.length; a++) {
  promises.push($scope.calcEmFacPurElec($scope.countries[a], 'ton/kWh', 'CO2e')); // new promise chain that does all of the work for that country
}

$q.all(promises).then(function(arrayofCountryEmFacPurElecs) {console.log('all countries completed')});

【讨论】:

  • 谢谢康拉杰。回复晚了非常抱歉。我忙于其他工作,然后在度假,然后生病(ugh)。您的回复很有帮助,我正在研究解决方案,但还没有完全理解。如果我无法获得它,我将尝试发布一个具有我在未来几天尝试实现的特定功能的 jsfiddle。也许您可以帮助我以这种方式最终确定解决方案。再次感谢您。
  • @Mitch 很高兴这很有帮助 - 当您有更具体的事情时,很乐意尝试并提供帮助。不要忘记您可以为有用的答案投票并将其标记为正确;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2017-10-30
  • 2022-12-08
  • 2017-12-16
  • 2015-09-09
  • 2018-04-28
  • 2017-10-22
相关资源
最近更新 更多