Handle multiple synchronous independent http request Angular 2

徘徊边缘 提交于 2020-01-23 16:48:56

问题


I'm trying to build an angular 2 application which shows multiple charts to users and i'm facing a real problem with handling multiple parallel independent http request , i got 505 response when i call more then 2 requests in 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]);

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

回答1:


You are typing up all service calls inside the subscription instead you should have them independent as below,

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

Update based on comment:

Raising another request after completion of the previous

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


来源:https://stackoverflow.com/questions/44864803/handle-multiple-synchronous-independent-http-request-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!