Suppose I have something like this:
submit(): void {
this.loading = true;
dataLength?: number = untracked(this.dataService.data)?.length;
this.dataService.addData(this.dataForm.value);
effect(
() => {
if (this.dataLength !== this.dataService.data().length) {
this.loading = false;
//Show success message
}
},
{ injector: this.injector }
);
}
This is pretty much self explanatory - once the user clicks on a submit button, a progress spinner is popped up. When the data changes as a result of the addData()
call - the progress spinner disappears. Cool, it works.
However, it raises two main question,
- What happens if
addData()
throws an error? - What happens if I have another source that updates
dataService.data
, for example, a polling mechanism?
In the context of RxJS we have catchError
for dilemma #1 (that would also do this.loading = false
but would show an error message instead). For the second part, we can use skipWhile
on the other observer to disable the polling while this.loading
is true.
What are the correct ways to handle those kind of issues?