I have a http call inside a forloop. The APICall will return a HTML response which i am using it render on the page.
Now I also have a requirement where after all API calls's are completed i have to perform some logic to update some data on the backend.
I understand we can use a forkjoin operator to capture array of observables and then update the BE data. But i am unable to understand how to handle the requirement that has to be completed on each subscription.
for(let item of Items){
this.myService
.getMyItemData(item.key)
.pipe(
takeUntil(this.destroyed),
distinctUntilChanged(),
catchError((e: Error) => {
this.logger.logError('Error loading', e);
return of('An Error Occurred');
})
).subscribe((resp) => {
//How can i handle this subscription when using forkjoin ??
this.elementRef.nativeElement.html = resp;
})
}
Now after getting all the itemData i want to perform an update for the Backend. For this i am thinking of using a forkJoin to capture all observables data. But at the same time i want to use the subscription code to render the HTML. Can someone help how can i achieve that.
Reference for my forkJoin Code*
let arrayOfObservables = Items.map((item) => this.myService
.getMyItemData(item.key))
let dataSource = Rx.Observable.forkJoin(arrayOfObservables);
dataSource.subscribe((resp) => {
// update my BE data
})