In my component, I have a process that retrieves data from an API. To indicate whether the data is being loaded or not, I use a member variable called loading
. Once the data retrieval process is complete, I set loading
to false to hide the loading icon. This should happen even if there was an error during the process.
Currently, my implementation looks like this:
export class MyComponent implements OnInit {
loading = true;
data;
constructor(private dataService: DataService) { }
ngOnInit() {
this.dataService.getData().subscribe(data => {
this.data = data;
this.loading = false;
},
() => {
this.loading = false;
});
}
}
I'm exploring ways to avoid repeating this.loading = false
. If I were working with Promises, I would utilize the Promise.finally()
callback for this purpose - however, I am using RxJS. Is there an equivalent in RxJS or a more efficient way to achieve this?