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

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

问题描述

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

I have multiple api requests which can run in parallel.

getlocations、getstates、get Regions 可以并行发出请求.然而,一旦我从 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 个 promise(尤其是 1 和 3)中的每一个,以便 this.locations、this.states 和 this.territories 都在$q.all .then - 您不必这样做,但我认为它使代码更具可读性

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.all

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 承诺返回一个对象而不是我假设它是一个数组 - 然后将内部 $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)))

实际上,上面的代码同样适用于数组或对象:p

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

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

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