I am working on Angular 6+ web-app using rxjs 6+ .
There is a set of services(all are different and make different http calls) . Basically these services are used to initialize the app.
My requirement is to call these services in parallel and wait untill all of them are finished . Once finished , I need to process the responses received . I have studied a lot on this and found that forkJoin and combineLatest can be my friend. And i have implemented the same in the following way :
getInitialData() {
this._authService.openCorsConnection().subscribe(res => {
forkJoin(
this._authService.x(),
this._tService.getSystemDate(),
this._tService.getTemp(),
this._tService.getSettings()
).pipe(
catchError( err => {
console.log('gor error',err);
if (err.status == 401) {
// this._router.navigateByUrl('/unauthorize');
return EMPTY;
} else {
return throwError(err);
}
}),
map(([x,y,z,p])=>{
console.log('sucesss',{x,y,z,p});
return {x,y,z,p}
}),
finalize(()=>{
console.log('Executing Finally');
processData();
})
);
});
}
But These are not executing . Here are the services :
x(): Observable<any> {
const xUrl = EnvironmentConfiguration.endPointConfig.xServiceEndpoint;
let serviceMethod: String = 'x';
let requestObj: any = this._utilHelperSvc.getRequestObject(xUrl , { "y": "" }, serviceMethod);
return this._http.post<any>(requestObj.url,{ "pid": "" } , requestObj)
.pipe(
catchError(this.handleError('x', {}))
);
}
Do i need to modify services ? I am not able to figure out how to solve this.
Can anybody please Suggest some better solution or different approach for this ?
Thanks.