Both observables and promises help us work with asynchronous functionality in JavaScript. Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time.
Here are the differences in concept between Observables
and Promises
.
Observables | Promises |
---|---|
Emit multiple values over a period of time. | Emit a single value at a time. |
Are lazy: they’re not executed until we subscribe to them using the subscribe() method. | Are not lazy: execute immediately after creation. |
Have subscriptions that are cancellable using the unsubscribe() method, which stops the listener from receiving further values. | Are not cancellable. |
Provide the map, filter, reduce, retry, retryWhen, and so many other RxJS operators, that makes it easy to deal with Observables. | Don’t provide any operations. |
Deliver errors to the subscribers. | Push errors to the child promises. |
code snippets/examples of a few operations defined by observables and promises.
Top comments (0)