用$ q.all在诺言中调用诺言 [英] calling a promise within a promise , with $q.all

查看:116
本文介绍了用$ q.all在诺言中调用诺言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个可以并行运行的api请求.

I have multiple api requests which can run in parallel.

getlocations,getstates,get领地可以并行发出请求.但是,一旦我从getstates()调用获得了状态列表,就需要为每个单独的状态发出api请求并解析状态对象.

getlocations, getstates, get territories can make a request in parallel. however once i have the states list from getstates() call, i need to make an api request for each individual state and parse the state object.

这是我到目前为止的代码,以这种方式在诺言中调用诺言是否可以?

Here is the code that i have so far, Is it ok to call a promise within a promise this way ?

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

如果不是,打电话给getStatesummary()的最佳方法是什么?

if not , whats the best way to make teh getStatesummary() call ?

推荐答案

我简化了3个承诺中的每一个(尤其是1和3),因此this.locations,this.states和this.territories均在以下位置更新$ q.all.然后-您不必这样做,但我认为它使代码更具可读性

I'd simplify each of the 3 promises (especially 1 and 3), so that this.locations, this.states and this.territories are all updated in the $q.all .then - you don't have to, but I think it makes the code a little more readable

接下来,将所有3个Promise分配给一个变量(以下代码中的p1,p2,p3)

Next, assign all 3 promises to a variable (p1, p2, p3 in the code below)

下一个问题是等待所有状态API调用完成-在那里需要另外$ q.

The next issue is waiting on all the states API calls to complete - another $q.all required there

所有这些,再加上一点ES6优势,您就会得到

All up, with a little extra ES6 goodness, you get

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承诺返回一个Object而不是我的假设,即它是一个数组,则将内部的$q.all替换为

If the getAllStates promise returns a Object instead of my assumption that it's an array - then replace the inner $q.all with

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

实际上,以上代码同样适用于Array或Object:p

Actually, the above code would work for Array or Object equally :p

这篇关于用$ q.all在诺言中调用诺言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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