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

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

问题描述

一旦所有三个都完成,我有 3 个并行承诺或 api 请求,我需要根据第二个承诺调用另一个 api 请求,然后最后调用 .then( of $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.(最后.然后)

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

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

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

推荐答案

内部 $q.allforEach 循环的每次迭代中被调用,并获得为参数在该 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( ......

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

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