首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理多个同步独立http请求角2

处理多个同步独立http请求角2
EN

Stack Overflow用户
提问于 2017-07-01 19:46:33
回答 1查看 875关注 0票数 1

我试图构建一个向用户显示多个图表的Range2应用程序,而且我在处理多个并行独立http请求时遇到了实际问题,当我在ngOnInit中调用超过2个请求时,我得到了505个响应。

代码语言:javascript
复制
    this._service.getPlant().subscribe(plants => {
    for (var i = 0; i < plants.length; i++)
        for (var name in plants[i]) {
            this.plants.push(plants[i][name]);
            console.log(plants[i][name]);

        }
    this._service.getDept().subscribe(depts => {

        for (var i = 0; i < depts.length; i++)

            for (var name in depts[i]) {
                this.depts.push(depts[i][name]);
                console.log(depts[i][name])

            }
        this._service.getMachines().subscribe(machines => {
            for (var i = 0; i < machines.length; i++)
                for (var MachineName in machines[i]) {
                    machines.push(machines[i][MachineName]);
                    // console.log(machines[i][MachineName]) ;
                }
        });
    });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-01 19:53:58

您正在subscription中键入所有服务调用,而应该让它们独立于下面,

代码语言:javascript
复制
this._service.getPlant().subscribe(plants => {
    for (var i=0; i<plants.length; i++)
        for (var name in plants[i]) {
            this.plants.push(plants[i][name]);  
            console.log(plants[i][name]) ;
        }
});
this._service.getDept().subscribe(depts => {
    for (var i=0; i<depts.length; i++)
        for (var name in depts[i]) {  
            this.depts.push(depts[i][name]);
            console.log(depts[i][name])
        }
});
this._service.getMachines().subscribe(machines => {
    for (var i=0; i<machines.length; i++)
        for (var MachineName in machines[i]) {
                machines.push(machines[i][MachineName]);  
                // console.log(machines[i][MachineName]) ;
        }
 });

根据评论更新:

在完成上一个后引发另一个请求

代码语言:javascript
复制
ngOnInit(){
    this._service.getPlant().subscribe(plants => {
        for (var i=0; i<plants.length; i++)
            for (var name in plants[i]) {
                this.plants.push(plants[i][name]);  
                console.log(plants[i][name]) ;
            }
    },(error)=>{},
        ()=>{
        /////////////////////////////
        // Completion event handler
        /////////////////////////////
            this.getDepartments();
        });

    private getDepartments(){
        this._service.getDept().subscribe(depts => {
            for (var i=0; i<depts.length; i++)
                for (var name in depts[i]) {  
                    this.depts.push(depts[i][name]);
                    console.log(depts[i][name])
                }
        },(error)=>{},
        ()=>{
        /////////////////////////////
        // Completion event handler
        /////////////////////////////
            this.getMachines();
        });
    }
    private getMachines(){

        this._service.getMachines().subscribe(machines => {
            for (var i=0; i<machines.length; i++)
                for (var MachineName in machines[i]) {
                        machines.push(machines[i][MachineName]);  
                        // console.log(machines[i][MachineName]) ;
                }
         });
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44864803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档