Observable vs Promise | When to use ObservableEDIT
Observable vs Promise | Which is better?
Observable vs Promise | When to use Promise
Observable vs Promise | When to use Observable
When you want to consume multiple values
Observables are able to emit multiple values. Observables are designed to evaluate streams of data over time.
Whether its an API call or a simple iteration, Observables can use the same instance to subscribe to multiple events.
When you don't mind working with RxJs and other dependencies
While it's possible to create your own implementation of the observer design pattern, most applications leverage the RxJs implementation of Observables.
If your project is Node based or you are using React/Angular then it's a lot easier to implement Observables with RxJs using npm etc.
When you want the ability to cancel async activity (before it completes)
You can effectively cancel an Observable by unsubscribing. When you unsubscribe to an Observable, you can terminate any pending async activity. This is not possible with a Promise where a single event is always rejected/resolved.
When you want to work with "streams" of data
Using Observables follows reactive programming techniques. Observables allow you to respond to both sync/async events as they happen over time.
When you're using Angular
Angular leverages the RxJs library. Using Observables in Angular is almost unavoidable and should be embraced.
When you want your code to behave synchronously
Unlike a Promise, an Observable can behave both sync and async.
When you want to explore operators
The RxJs library includes a bunch of operators. Operators allow you to gracefully mutate streams of data as values are emitted over time.
Some of the more popular operators include filter(), map(), and mergeMap().