多个诺言并行运行,$ q.all是否需要链接? [英] multiple promises running in parallel, $q.all needs chaining?

查看:56
本文介绍了多个诺言并行运行,$ q.all是否需要链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

三个都完成后,我有3个并行的Promise或api请求,我需要根据第二个Promise调用另一个api请求,然后最终调用$ q.all

I have 3 parallel promises or api requests once all teh three are done, I need to call another api request based on the second promise and then finally call .then( of $q.all

这是代码

 getAllLocations() {
    //make a promise call for all here .
    var promise = [];

    ̶p̶r̶o̶m̶i̶s̶e̶.̶p̶u̶s̶h̶(̶t̶h̶i̶s̶.̶g̶e̶t̶A̶l̶l̶L̶o̶c̶a̶t̶i̶o̶n̶s̶(̶I̶d̶)̶.̶t̶h̶e̶n̶(̶
    promise.push(this.getLocations(Id).then(
        (locationsData) => {
            this.locations = locationsData;
        }));

    promise.push(this.getAllStates(Id).then(
        (resp) => {
            this.states = resp.data;
        }));

    promise.push(this.getTerritories(Id).then(
        (resp) => {
            this.utilizations = resp.data;
        }));

    $q.all(promise).then(() => {
        var nodePromise = [];
        angular.forEach(this.states, function(node) {
            var nodeId = node.Id;
            nodePromise.push(this.getNodeHealthSummary(nodeId).then(
                (resp) => {
                    node.healthStatus = resp.data.operationalStatus;
                }));
            this.$q.all(nodePromise).then(() => {
                var index = this.states.indexOf(node);
                this.states.splice(index, 1, angular.copy(node));
            });
        },this);
    }).then(() => {
        for (var i = 0; i < this.locations.length; i++) {
            //do something here with this.states
        }
        this.gridData = this.locations;
    });
}

当我在this.locations的for循环中时,我需要使用healthStatus属性更新的this.states. (last.then)

I need this.states updated with healthStatus property when i am in the for loop of this.locations. ( the last.then )

但是,我看到在为每个状态设置node.healthStatus属性之前,this.locations for循环已提前完成.

However , i see that this.locations for loop is done ahead before the node.healthStatus property is set on each state.

这怎么办?使用Promises代替$ q是可以的.请让我知道我怎么能做到这一点,我已经全部尝试了

How can this be done? Using Promises instead of $q is fine. Please let me know how can i achieve this , i have tried all in vain

推荐答案

forEach循环的每次迭代中调用内部$q.all,并将在该forEach循环中填充的数组作为参数.这显然是不对的.只能调用一次,其结果应该是then回调的返回值.

The inner $q.all is called in each iteration of the forEach loop, and gets as argument the array that is being populated during that forEach loop. This is obviously not right; it should be called only once, and its result should be the return value of the then callback.

因此,而不是此块:

$q.all(promise).then(() => {
    var nodePromise = [];
    angular.forEach(this.states, function(node) {
        var nodeId = node.Id;
        nodePromise.push(this.getNodeHealthSummary(nodeId).then(
            (resp) => {
                node.healthStatus = resp.data.operationalStatus;
            }));
        this.$q.all(nodePromise).then(() => {
            var index = this.states.indexOf(node);
            this.states.splice(index, 1, angular.copy(node));
        });
    },this);
}).then( ......

执行此操作:

$q.all(promise).then(() => {
    return $q.all(this.states.map((node, index) => {
        return this.getNodeHealthSummary(node.Id).then(resp => {
            node.healthStatus = resp.data.operationalStatus;
            this.states[index] = angular.copy(node);
        });
    }));
}).then( ......

这篇关于多个诺言并行运行,$ q.all是否需要链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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