等待所有承诺解决 [英] Wait for all promises to resolve

查看:128
本文介绍了等待所有承诺解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个情况我有一个未知长度的多个承诺链。我想在所有的锁链都被处理了一些操作来运行。是,即使可能吗?下面是一个例子:

  app.controller('MainCtrl',函数($范围,$ Q $超时){
    变种1 = $ q.defer();
    变种2 = $ q.defer();
    变种3 = $ q.defer();    所有VAR = $ q.all([one.promise,two.promise,three.promise]);
    all.then(allSuccess);    函数成功(数据){
        的console.log(数据);
        返回+链接的数据;
    }    功能allSuccess(){
        的console.log(解决了所有的承诺)
    }    one.promise.then(成功)。然后(成功);
    two.promise.then(成功);
    three.promise.then(成功)。然后(成功)。然后(成功);    $超时(函数(){
        one.resolve(一完成);
    },的Math.random()* 1000);    $超时(函数(){
        two.resolve(两完成);
    },的Math.random()* 1000);    $超时(函数(){
        three.resolve(三完成);
    },的Math.random()* 1000);
});

在这个例子中,我设置了一个 $ q.all()的承诺一,二,三将在一些随机时间得到解决。我再加入承诺到一个和三个目的。我想在所有来当所有的连锁店都解决了解决。这是当我运行这个code的输出:

 一DONE
1 doneChained
2 DONE
3 DONE
所有的承诺已解决
3 doneChained
3 doneChainedChained

有没有办法等待链解决?


解决方案

  

我想所有的解决当所有的连锁店都解决了。


当然,那么就通过每个链的承诺到所有(),而不是最初的承诺:

  $ q.all([one.promise,two.promise,three.promise]),然后(函数(){
    的console.log(所有最初的承诺已解决);
});VAR onechain = one.promise.then(成功)。然后(成功)
    twochain = two.promise.then(成功)
    threechain = three.promise.then(成功)。然后(成功)。然后(成功);$ q.all([onechain,twochain,threechain]),然后(函数(){
    的console.log(解决了所有的诺言);
});

So I have a situation where I have multiple promise chains of an unknown length. I want some action to run when all the CHAINS have been processed. Is that even possible? Here is an example:

app.controller('MainCtrl', function($scope, $q, $timeout) {
    var one = $q.defer();
    var two = $q.defer();
    var three = $q.defer();

    var all = $q.all([one.promise, two.promise, three.promise]);
    all.then(allSuccess);

    function success(data) {
        console.log(data);
        return data + "Chained";
    }

    function allSuccess(){
        console.log("ALL PROMISES RESOLVED")
    }

    one.promise.then(success).then(success);
    two.promise.then(success);
    three.promise.then(success).then(success).then(success);

    $timeout(function () {
        one.resolve("one done");
    }, Math.random() * 1000);

    $timeout(function () {
        two.resolve("two done");
    }, Math.random() * 1000);

    $timeout(function () {
        three.resolve("three done");
    }, Math.random() * 1000);
});

In this example, I set up a $q.all() for promises one, two, and three which will get resolved at some random time. I then add promises onto the ends of one and three. I want the all to resolve when all the chains have been resolved. Here is the output when I run this code:

one done 
one doneChained
two done
three done
ALL PROMISES RESOLVED
three doneChained
three doneChainedChained 

Is there a way to wait for the chains to resolve?

解决方案

I want the all to resolve when all the chains have been resolved.

Sure, then just pass the promise of each chain into the all() instead of the initial promises:

$q.all([one.promise, two.promise, three.promise]).then(function() {
    console.log("ALL INITIAL PROMISES RESOLVED");
});

var onechain   = one.promise.then(success).then(success),
    twochain   = two.promise.then(success),
    threechain = three.promise.then(success).then(success).then(success);

$q.all([onechain, twochain, threechain]).then(function() {
    console.log("ALL PROMISES RESOLVED");
});

这篇关于等待所有承诺解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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