So, after 2 years of using Angular, here are some things i found out:
- There is this library called RxJS - https://rxjs-dev.firebaseapp.com/guide/overview, which is written in TypeScript (superset of JavaScript). It is like one of thee best libraries for JavaScript.
Basically, it makes event-driven apps a breeze in JS by using the Observable paradigm. A Promise can only return a value once and is async only. Observables can be used to return a single value OR multiple values and can be sync or async (depending on how you use it), you can add pipes and operators to it and etc; it is far more feature-rich than promises.
An Observable listens to 3 types of events: next, error, and complete. Next means an event with data was emitted, error means there was an error with the event stream, and complete means the event stream has ended.
Now, just because there was an error emitted, does not mean the event stream stopped. An event stream is only stopped when the complete event happens. Also, an observable can unsuscribe, meaning it will stop listening to the event stream.
- The Http service is built using RxJS. They basically wrapped it over the old XmlHttpRequest way of making requests.
When you make a request using Angular's Http service, it automatically completes the observable, so there is no needed to call unsuscribe on the observable.
I would say Observables are far better to use over Promises; i don't see RxJS leaving any time soon.