Javascript 多个承诺并行运行,$q。所有承诺都需要链接?

Javascript 多个承诺并行运行,$q。所有承诺都需要链接?,javascript,angularjs,promise,angular-promise,es6-promise,Javascript,Angularjs,Promise,Angular Promise,Es6 Promise,我有3个并行承诺或api请求一旦这三个都完成了,我需要根据第二个承诺调用另一个api请求,然后最后调用 这是密码 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

我有3个并行承诺或api请求一旦这三个都完成了,我需要根据第二个承诺调用另一个api请求,然后最后调用

这是密码

 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;
    });
}
getAllLocations(){
//向这里的所有人承诺。
var promise=[];
p-8222P p-p-p-p p-p-p-p-p-p-p-p p-p p-p p-p p-p p-p p-p p-p-p-p p-r-rγγγ-p p-p,p-r,r-p-p-r,o-p-p-p-p-r,o,r-p-p-r,o,o,o,o,o,o-p-p-p,r,r,o,o,o,o,o-p-p-p-p,o,o,o,o,o,o,o,o,o,o,p-p-p-p,oγγγγγγγγ,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,0 0 0 0 0 0 0 0 0 0,o,o,o,o,γγγγγγγγ,p,p,̶s̶(̶,i̶,d̶).̶t̶,h̶,e̶(̶
promise.push(这个.getLocations(Id)。然后(
(位置数据)=>{
此位置=位置数据;
}));
promise.push(this.getAllStates(Id)),然后(
(resp)=>{
this.states=resp.data;
}));
promise.push(这个.getTerritories(Id)。然后(
(resp)=>{
本实用性=相应数据;
}));
$q.all(承诺)。然后(()=>{
变量nodePromise=[];
angular.forEach(this.states,function(node)){
var nodeId=node.Id;
nodePromise.push(这个.getNodeHealthSummary(nodeId)),然后(
(resp)=>{
node.healthStatus=resp.data.operationalStatus;
}));
这个.$q.all(nodePromise)。然后(()=>{
var index=this.states.indexOf(节点);
这个.states.splice(索引,1,角度.copy(节点));
});
},这个);
}).然后(()=>{
对于(var i=0;i
当我在this.locations的for循环中时,需要用healthStatus属性更新this.states。(最后一个.then)

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


如何做到这一点?使用承诺而不是$q很好。请让我知道我如何做到这一点,我已经尝试了所有的方法,但都没有成功。内部的
$q。在
forEach
循环的每次迭代中调用all
,并获取在
forEach
循环期间填充的数组作为参数。这显然不是right;应该只调用一次,其结果应该是
然后
回调的返回值

因此,与此块不同:

$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
forEach
循环的每次迭代中都被调用,并获取在
forEach
循环期间填充的数组作为参数。这显然是不对的;应该只调用一次,其结果应该是
然后
回调的返回值

因此,与此块不同:

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

代码是否对
getAllLocations
函数进行递归调用?我很确定您的意思是将
$q.all
放在循环之外?尝试使用
map
而不是
forEach
typo,对于getAllLocations,它的getLocations()在promise内部,我在实际代码中正确地使用了它。如果我将$q放入外部,我需要将数组与节点拼接并复制新节点,我不能将其放入foreach循环外部,因为节点引用不是有代码对
getAllLocations
函数进行递归调用吗?我很确定你是想将
放入$q。所有
在循环之外?尝试使用
map
而不是
forEach
typo,对于getAllLocations,其getLocations()在promise内部,我在实际代码中正确地使用了它。如果我将$q放在外部,我需要将数组与节点拼接并复制新节点,我不能在foreach循环外部对其进行di,因为节点引用不存在