AngularJS 1.0.7:使用$ resources嵌套并行承诺 [英] AngularJS 1.0.7: Nesting parallel promises with $resources
问题描述
这篇文章扩展了前一个已经解决的问题。请看看它,以获取上下文:使用AngularJS 1.0.7中的$资源嵌套承诺
this post extends a previous one already solved. Please see it, to get in context: Nesting promises with $resources in AngularJS 1.0.7
考虑到这种方法,我想对函数进行并行调用,当两者都是完成然后运行带有结果的searchBoats函数。我想我可以嵌套承诺,但我也认为它可以并行完成,也许可以使用$ q.all。不幸的是,这个承诺的主题让我感到困惑,我不知道该怎么做。
With that approach in mind, I would like to make a parallel call to functions and when both are completed then run the searchBoats function with the results. I think I can nest the promises, but I also think it can be done in parallel, maybe with $q.all. Unfortunately, this promises topic is confusing for me and I don´t know how to do it.
以上一篇文章为例,我想做的事情如下: / p>
Taking the previous post example, I would like to do something like:
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()
,即可返回承诺:
As in your earlier question, there is no need to use a deferred
here. Just use $q.all()
, which returns a promise:
var parseURL = function() {
$q.all([parseBoatType(), parseDestination()])
.then(function (results) {
console.log(results);
searchBoats(results);
});
};
parseURL();
这篇关于AngularJS 1.0.7:使用$ resources嵌套并行承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!