Issue
I have 4 functions similar to the one below. Right now I'm calling them one at a time on ngOnInit() but I'm looking for a cleaner way to call all 4 functions (GET HTTP Requests) so that they show spinner... all run parallel... and then close spinner and handle any errors. This is for a project using angular 7.
getCustomers() {
return this.apiService.getCustomers()
.subscribe(
(data: any) => {
this.partners = data;
this.loading = false;
},
error => {
console.log(error);
this.loading = false;
}
);
}
Solution
You need to combine all observables from your service into a single observable and then subscribe to the newly created observable. to do this you can use RxJs operator forkJoin
the fork join subscription will wait for all observables to complete and then emit the values to the subscription method -note that mean single success and single error for all since the observable will only emit once
you can also use RxJs Operator combineLatest this will create single observable from your list of observers and emit a value every time one of the observables complete. the value will be array of all last values emmited from your observers or null in case an observer not finished. this will allow you to handle error state for each api call but will also fire the subscription event multiple times.
Answered By - Ayman Ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.