I currently have 15 HTTP requests being sent to the API individually. Instead of waiting for all requests to finish processing (especially one that can take a few minutes), I want to handle responses as they come in.
On the service side:
findOneByOne(): Observable<any> {
const calls = this.getCardsPath().map(el => this.getPromises(el));
return Observable.forkJoin(calls)
.map(res => {
const tab = [];
for (let i = 0; i < res.length; i++) {
tab.push(this.checkInfoService(res[i].json()));
}
return tab;
});
}
getPromises(str: String): Promise<any> {
return this.requester.obtain({
restUrl: "/administration/" + str,
method: RequestMethod.Get
})
.toPromise()
.then(res => res)
.catch(err => err);
}
On the component side:
displayDashboardInfoService() {
if (this.featuresFlag.getCurrentVersion() !== "1.08" && this.featuresFlag.getCurrentVersion() !== "-1") {
this.busy = this.dashboardInfoService.findAll()
.then((res: DashboardInfo[]) => this.findPaths(res))
.then((res: DashboardInfo[]) => this.loadItems(res))
.catch((err: any) => {
if (environment.debugLevel >= 3) console.error(err);
});
}
else {
this.dashboardInfoService.findOneByOne()
.subscribe((res) => {
const tab = [];
for (let i = 0; i < res.length; i++) {
tab.push(res[i][0]);
}
this.findPaths(tab);
this.loadItems(tab);
});
}
}
Thank you :)