angular $q,如何在for循环内和之后链接多个promise [英] angular $q, How to chain multiple promises within and after a for-loop

查看:28
本文介绍了angular $q,如何在for循环内和之后链接多个promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 for 循环,每次迭代都调用异步函数.

I want to have a for-loop which calls async functions each iteration.

在 for 循环之后,我想执行另一个代码块,但不是在 for 循环中先前的所有调用都得到解决之前.

After the for-loop I want to execute another code block, but not before all the previous calls in the for-loop have been resolved.

我目前的问题是,要么在所有异步调用完成之前执行 for 循环之后的代码块,要么根本不执行.

My problem at the moment is, that either the code-block after the for-loop is executed before all async calls have finished OR it is not executed at all.

带有 FOR 循环的代码部分及其后的代码块(完整代码请参见 fiddle):

The code part with the FOR-loop and the code block after it (for complete code, please see fiddle):

[..]
function outerFunction($q, $scope) {
    var defer = $q.defer();    
    readSome($q,$scope).then(function() {
        var promise = writeSome($q, $scope.testArray[0])
        for (var i=1; i < $scope.testArray.length; i++) {
             promise = promise.then(
                 angular.bind(null, writeSome, $q, $scope.testArray[i])
             );                                  
        } 
        // this must not be called before all calls in for-loop have finished
        promise = promise.then(function() {
            return writeSome($q, "finish").then(function() {
                console.log("resolve");
                // resolving here after everything has been done, yey!
                defer.resolve();
            });   
        });        
    });   

    return defer.promise;
}

我创建了一个 jsFiddle,可以在这里找到 http://jsfiddle.net/riemersebastian/B43u6/3/.

I've created a jsFiddle which can be found here http://jsfiddle.net/riemersebastian/B43u6/3/.

目前看起来执行顺序没问题(见控制台输出).

At the moment it looks like the execution order is fine (see the console output).

我的猜测是,这仅仅是因为每个函数调用都立即返回而没有做任何实际工作.我试图用 setTimeout 延迟 defer.resolve 但失败了(即从未执行过最后一个代码块).您可以在 fiddle 的 outcommented 块中看到它.

My guess is, that this is simply because every function call returns immediately without doing any real work. I have tried to delay the defer.resolve with setTimeout but failed (i.e. the last code block was never executed). You can see it in the outcommented block in the fiddle.

当我使用真正的写文件和读文件的函数时,最后一个代码块在最后一个写操作完成之前执行,这不是我想要的.

When I use the real functions which write to file and read from file, the last code block is executed before the last write operation finishes, which is not what I want.

当然,错误可能出在这些读/写函数之一中,但我想验证我在这里发布的代码没有任何问题.

Of course, the error could be in one of those read/write functions, but I would like to verify that there is nothing wrong with the code I have posted here.

推荐答案

你需要使用的是 $q.all 将多个 promise 组合成一个,只有在所有 promise 都解决后才会解决.

What you need to use is $q.all which combines a number of promises into one which is only resolved when all the promises are resolved.

在您的情况下,您可以执行以下操作:

In your case you could do something like:

function outerFunction() {

    var defer = $q.defer();
    var promises = [];

    function lastTask(){
        writeSome('finish').then( function(){
            defer.resolve();
        });
    }

    angular.forEach( $scope.testArray, function(value){
        promises.push(writeSome(value));
    });

    $q.all(promises).then(lastTask);

    return defer.promise;
}

这篇关于angular $q,如何在for循环内和之后链接多个promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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