AngularJS 1.0.7:嵌套并行承诺,并使用$资源



这篇文章扩展了先前已经解决的文章。请查看,以获取上下文:嵌套承诺在Angularjs 1.0.7

中使用$ Resources

考虑到这种方法,我想对功能进行并行调用,然后两者都完成后,然后以结果运行搜索板功能。我认为我可以筑巢的承诺,但我也认为可以并行完成,也许是$ q.all。不幸的是,这个承诺的话题使我感到困惑,我不知道该怎么做。

以上一篇文章的示例为例,我想做类似的事情:

var parseURL = function() {
  var deferred = $q.defer();
  var promise = deferred.promise;
  promise.then(function success(result) {
    console.log(result);
    searchBoats(result);
  });
  // Instead of resolve when parseBoatType promise is returned, I would like to   
  // wait for two promises
  parseBoatType().then(deferred.resolve);
  parseDestination().then(deferred.resolve);
  // of course, with this code deferred will be resolved when first promise 
  // comes. Is it possible to use $q.all here?
};
parseURL();

与您之前的问题一样,这里无需在此处使用deferred。只需使用$q.all(),它返回承诺:

var parseURL = function() {
  $q.all([parseBoatType(), parseDestination()])
      .then(function (results) {
          console.log(results);
          searchBoats(results);
      });
};
parseURL();

最新更新