When should we use Observer and Observable?

An interviewer asked me: What is Observer and Observable and when should we use them? I wasn’t aware of these terms, so when I got back home and started Googling about Observer and Observable, I found some points from different resources: 1) Observable is a class and Observer is an interface. 2) The Observable class … Read more

Return an empty Observable

The function more() is supposed to return an Observable from a get request export class Collection { public more = (): Observable<Response> => { if (this.hasMore()) { return this.fetch(); } else { // return empty observable } }; private fetch = (): Observable<Response> => { return this.http.get(“some-url”).map((res) => { return res.json(); }); }; } In … Read more

Delegation: EventEmitter or Observable in Angular

I am trying to implement something like a delegation pattern in Angular. When the user clicks on a nav-item, I would like to call a function which then emits an event which should in turn be handled by some other component listening for the event. Here is the scenario: I have a Navigation component: import … Read more

Angular/RxJS When should I unsubscribe from `Subscription`

When should I store the Subscription instances and invoke unsubscribe() during the ngOnDestroy life cycle and when can I simply ignore them? Saving all subscriptions introduces a lot of mess into component code. HTTP Client Guide ignore subscriptions like this: getHeroes() { this.heroService.getHeroes() .subscribe( heroes => this.heroes = heroes, error => this.errorMessage = <any>error); } … Read more