多次调用一个 promise 函数,直到满足另一个 promise 函数的条件 [英] Call a promise function multiple times until condition met from another promise function

查看:27
本文介绍了多次调用一个 promise 函数,直到满足另一个 promise 函数的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码: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() 以便我不必放置 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 而不是普通的 JavaScript 超时,这样对 $scope 的任何更改都不会发生在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆