- Observable are similar to promises but with major difference that make them better.
- Observables is an ES7 feature which means you need to make use of an external library to use it today. RxJS is a good one. RxJS also provides Observable operators which you can use to manipulate the data being emitted. Some of these operators are:
- Map
- Filter
- Take
- Skip
- Debounce
- When we use observable we simply replace "then with subscribe".The value function is called for each value the observable emits
//subscribe to the observable .subscribe(products => this.products=products, error =>this.errorMessage=<any>error);
- It provides an third handler that is executed on completion . This option is usually not used
//subscribe to the observable .subscribe(products => this.products=products, error =>this.errorMessage=<any>error,
completeFn);
- The subscribe function returns a subscription. We can use this to call unsubscribe and cancel the subscription
Note:
- Observable are lazy and are not called unless we use subscribe
- http call are single asynchronous operations only one item is emitted that is the http response object
No comments:
Post a Comment