调用一个承诺内的承诺,以$ q.all [复制](calling a promise within

2019-10-30 03:31发布

这个问题已经在这里有一个答案 :

我有一种能够并行运行多个API请求。

的getLocations,GETSTATES,获得领地可以并行的请求。 然而,一旦我从GETSTATES状态列表()调用,我需要为每个单独的状态的API请求,并解析状态对象。

下面是我到目前为止的代码,它是确定调用一个承诺中承诺这种方式?

  getAllLocations() {
    //make a promise call for all here .
    var promise = [];
    //first prmise
    promise.push(this.getAllLocations(Id).then(
        (locationsData) => {
            this.locations = locationsData;
        }));
    //second promise
    promise.push(this.getAllStates(Id).then(
        (resp) => {
            this.states = resp.data;
            //for each state , need to make an api call and add to the state info
            angular.forEach(this.states, function(state){
                var nodeId = state.Id;
                this.getStateSummary(nodeId).then((resp) => {
                    state.healthStatus = resp.data.operationalStatus;
                });
            },this)
        }));
    //third promise
    promise.push(this.getAllterritories(Id).then(
        (resp) => {
            this.territories = resp.data;
        }));
    //after all promises parse teh location data
    $q.all(promise).then(() => {
        for (var i = 0; i < this.locations.length; i++) {
            //do something with with location here
        }
        this.gridData = this.locations;
    });
}

如果没有,什么使TEH getStatesummary()调用的最好方法?

Answer 1:

我想简化各3个承诺(特别是1和3),使this.locations,this.states和this.territories都在更新$ q.all。那么 - 你没有,但我认为它使代码更可读

接着,将所有3个许诺给一个变量(P1,P2,P3在下面的代码)

接下来的问题是在所有的州API调用等待完成 - 需要有另一种$ q.all

所有了,有一点点额外的ES6的善良,你会得到

getAllLocations() {
    //first prmise
    const p1 = this.getAllLocations(Id);
    //second promise
    const p2 = this.getAllStates(Id).then(resp => 
        //for each state , need to make an api call and add to the state info
        $q.all(resp.map(state => this.getStateSummary(state.Id).then(resp => state.healthStatus = resp.data.operationalStatus)))
        .then(() => resp) // so we return data for this.states
    );
    //third promise
    const p3 = this.getAllterritories(Id).then(resp => resp.data);
    //after all promises parse teh location data
    $q.all([p1, p2, p3]).then(([locations, states, territories]) => {
        this.locations = locations;
        this.states = states;
        this.territories = territories;
        for (var i = 0; i < locations.length; i++) {
            //do something with with location here
        }
        this.gridData = this.locations;
    });
}

如果getAllStates承诺返回一个对象,而不是我的假设,它是一个数组-再更换内$q.all

        $q.all(Object.entries(resp).map(([key, state]) => this.getStateSummary(state.Id).then(resp => state.healthStatus = resp.data.operationalStatus)))

其实,上面的代码将工作数组或对象相等:P



Answer 2:

您可以使用诺链发送后,另一个抓一个承诺

 getAllLocations() {
     //make a promise call for all here .
     var promise = [];
     //first prmise
     this.getAllLocations(Id)
         .then(
             (locationsData) => {
                 this.locations = locationsData;
                 promise.push(this.locations);

                 return this.getAllStates(Id)
             }).then(
             (resp) => {
                 this.states = resp;
                 //for each state , need to make an api call and add to the state info
                 angular.forEach(this.states, function(state) {
                     var nodeId = state.Id;
                     return this.getStateSummary(nodeId);
                 }, this)
                 return this.getAllterritories(Id);
             }).then((resp) => {
             state.healthStatus = resp.data.operationalStatus;
             promise.push(state.healthStatus);
         }).then(
             (resp) => {
                 this.territories = resp.data;
                 promise.push(this.territories);
                 this.callAll(promise)
             })
 }


 callAll(promise) {
     //after all promises parse teh location data
     this.$q.all(promise).then(() => {
         for (var i = 0; i < this.locations.length; i++) {
             //do something with with location here
         }
         this.gridData = this.locations;
     });
 }


文章来源: calling a promise within a promise , with $q.all [duplicate]