It appears that you are attempting to send multiple http requests and process the responses only after all requests have been completed. This could be useful when needing to gather data from various sources before proceeding with further logic.
If this is the case, one approach could be using ReactiveX Observables and the forkJoin()
method to combine multiple Observables.
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class MultipleHttpService {
constructor(private http:Http) { }
// If any individual request fails, the entire operation will result in an error state.
getData0AndData1() {
return Observable.forkJoin(
this.http.get('/app/data0.json').map((res:Response) => res.json()),
this.http.get('/app/data1.json').map((res:Response) => res.json())
);
}
}
Subsequently, you can retrieve the combined data by subscribing to the observable:
// Code within your component or page ...
this.myMultipleHttpService.getData0AndData1()
.subscribe(
data => {
this.data0 = data[0]
this.data1 = data[1]
},
err => console.error(err)
);