多个承诺 运行 并行,$q.all 需要链接?

multiple promises running in parallel, $q.all needs chaining?

我有 3 个并行承诺或 api 请求,一旦这三个都完成,我需要根据第二个承诺调用另一个 api 请求,然后最后调用 .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 循环中时,我需要 this.states 更新 healthStatus 属性。 ( last.then )

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

如何做到这一点?使用 Promises 而不是 $q 就可以了。请让我知道我怎样才能做到这一点,我都试过了都是徒劳的

内部 $q.allforEach 循环的每次迭代中被调用,并获取在 forEach 循环期间被填充的数组作为参数。这显然是不对的;它应该只被调用一次,其结果应该是 then 回调的 return 值。

所以不是这个块:

$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( ......