I'm using the async
pipe in an ngFor to watch an Observable. The Observable is created by a service that hits my server, and on load time when ngFor loop is enumerated, the service correctly makes a call to the server.
Now for the part I don't understand: when any results come back everything happens as expected. But if the server responds with say, a 404 and no results are available for enumeration, the async
pipe causes the service to continue to fire requests, milliseconds apart. This is obviously no good. What's the design expectation here and how can I gracefully handle an Observable that returns an error while using an async pipe?
In the Component template:
<li *ngFor="let person of persons | async">{{person.id}}</li>
In the Component body:
get persons: Observable<Person[]> {
return this.personService.list();
}
In the Service:
list(): Observable<Person[]> {
if (this.persons && this.persons.length) {
return Observable.from([this.persons]);
} else {
return this.http.get('/person')
.map((response: Response) => response.json())
.map((data: Person[]) => {
this.persons = data;
return data;
})
.catch((error: any) => {
let errMsg = "some error..."
return Observable.throw(errMsg);
});
}
}
}