简体   繁体   English

多次调用一个promise函数,直到另一个promise函数满足条件

[英]Call a promise function multiple times until condition met from another promise function

I have the following code: http://jsfiddle.net/kyy4ey10/4/ 我有以下代码: 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"
});

If I put: 如果我放:

var deferred = $q.defer(); 

inside the callPromise() function, $scope.count doesn't get resolved to "done". callPromise()函数中, $scope.count不会解析为“完成”。

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(); 我该如何更改编写这两个函数的方式,然后调用callPromise()因此不必将var deferred = $q.defer(); outside? 外?

When you declare deferred outside and recursively call your function the innermost recursive call will resolve the deferred.promise from the outside. 当您在外部声明deferred并递归调用函数时,最内部的递归调用将从外部解析deferred.promise 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/ 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/ http://jsfiddle.net/pLns9mjw/1/

function callPromise() {  
    return getActiveTasks().then(function(){
        if($scope.count < 5) {
            return callPromise();
        }
    });
}

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. 另外,不确定实际要考虑的工作是$q.all ,它接受了一组承诺并在所有承诺完成后解决。 As long as your tasks were known in advance, that's easy if you just want to notify when they're all done. 只要事先知道您的任务,如果您只想通知它们何时完成,这很容易。

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. 如果正在进行的任务是动态的,并且可能会增加,则可以$q.all它们全部加成$q.all ,然后检查完​​成承诺后是否还有剩余,直到没有剩下。

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. 如果您只想执行一个持续状态,当您处于某个阈值时,该状态会向用户报告,那么可以使用类似您的超时方法。

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. 请注意,您应该使用$timeout而不是原始的JavaScript超时,这样对$scope任何更改都不会在Angular的摘要周期之外发生。

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

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