multiple promises running in parallel, $q.all need

2019-03-05 03:35发布

问题:

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

Here is the code

 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;
    });
}

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

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

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

回答1:

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.

So instead of this block:

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

Do this:

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