多次调用promise函数,直到从另一个promise函数满足条件 [英] Call a promise function multiple times until condition met from another promise function
问题描述
我有以下代码: http://jsfiddle.net/kyy4ey10/4/
$scope.count = 0;
function getActiveTasks() {
var deferred = $q.defer();
setTimeout(function() {
$scope.count++;
deferred.resolve();
},1000)
return deferred.promise;
}
// i want to put this inside the function, but it doesn't work
var deferred = $q.defer();
function callPromise() {
getActiveTasks().then(function(){
if($scope.count < 5){
callPromise();
}
else{
deferred.resolve()
}
})
return deferred.promise;
}
callPromise().then(function(){
$scope.count = "done"
});
如果我把:
var deferred = $q.defer();
callPromise()
函数, $ scope.count
未解析为已完成。
inside the callPromise()
function, $scope.count
doesn't get resolved to "done".
如何更改我编写这两个函数的方式,然后调用 callPromise()
所以我不知道t必须在外面放 var deferred = $ q.defer();
?
How can I change how I've written these two functions and then call callPromise()
so I don't have to put var deferred = $q.defer();
outside?
推荐答案
当您在外部声明 deferred
并递归调用您的函数时,最里面的递归调用将解析 deferred.promise
从外部。当它在内部声明时,只有最内部的承诺得到解决(但它永远不会被返回)。考虑这个小提琴,它说明了一个类似的变量范围问题:
When you declare deferred
outside and recursively call your function the innermost recursive call will resolve the deferred.promise
from the outside. When it's declared inside, only the inner-most promise gets resolved (but it's never returned). Consider this fiddle which kind of illustrates a similar variable scoping issue:
https://jsfiddle.net/sg6odtof/
var counter = 5;
var a = "outside";
function b() {
//If you uncomment this, the behavior of b is different.
//var a = "inside";
if (counter > 0) {
counter--;
b();
}
return a;
}
alert(b());
我猜你不喜欢函数之外的那个变量;因此我认为你想要做的是让外部递归返回内部递归的承诺。
I'm guessing you don't like that variable outside the function; therefore I think what you want to do is have the outer recursion return the inner-recursion's promise.
http://jsfiddle.net/pLns9mjw/1/
function callPromise() {
return getActiveTasks().then(function(){
if($scope.count < 5) {
return callPromise();
}
});
}
另外,不确定你要做什么才能真正考虑 $ q.all
在所有承诺完成后接收一系列承诺并解决。只要您的任务事先知道,如果您只是想在完成任务时通知就很容易。
Also, not sure what you're trying to do for real consider $q.all
which takes in an array of promises and resolves when all promises are done. As long as your tasks were known in advance, that's easy if you just want to notify when they're all done.
如果正在进行的任务是动态的,可能会被添加,你可以 $ q.all
他们,并检查是否有任何遗留下来,直到没有任何遗留。
If the ongoing tasks is dynamic which might get added, you could $q.all
them, and check to see if any are left when that promise is done until there are none left.
如果你想做一个持续的状态,当你达到某个阈值时会向用户报告你的超时方法就好了。
If you want to just do an ongoing status that reports to the user when you're at certain thresholds a timeout approach like yours is fine.
注意你应该使用 $ timeout
而不是vanilla JavaScript超时,以便对 $ scope $进行任何更改c $ c>不要在Angular的摘要周期之外发生。
Note you should use $timeout
instead of vanilla JavaScript timeouts so that any changes to the $scope
don't happen outside the Angular's digest cycle.
这篇关于多次调用promise函数,直到从另一个promise函数满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!