It seems to me like Observables are basically wrappers for promises with some helper methods. It also requires a huge library to go along with it. Do the benefits of these helper methods outweigh the cons of importing a library?
- The Observer and Observable interfaces provide a generalized mechanism for push-based notification, also known as the observer design pattern. The Observable object represents the object that sends notifications (the provider); the Observer object represents the class that receives them (the observer).Robert Harvey– Robert Harvey2016-05-22 04:40:59 +00:00Commented May 22, 2016 at 4:40
- More generally, RxJS) is a set of libraries for composing asynchronous and event-based programs using observable sequences and fluent query operators. Using RxJS, developers represent asynchronous data streams with Observables, query asynchronous data streams using our many operators, and parameterize the concurrency in the asynchronous data streams using Schedulers.. So I'd say they're a bit more than promises with helper methods.Robert Harvey– Robert Harvey2016-05-22 04:43:55 +00:00Commented May 22, 2016 at 4:43
- You can read more about observables and their advantages at reactivex.io/intro.htmlRobert Harvey– Robert Harvey2016-05-22 05:03:02 +00:00Commented May 22, 2016 at 5:03
- egghead.io/lessons/rxjs-rxjs-observables-vs-promisesManish Jain– Manish Jain2017-01-01 01:49:58 +00:00Commented Jan 1, 2017 at 1:49
1 Answer
Promises are often used to tackle problems with callbacks. Conceptually promises are futures, so the problems with futures that are mentioned in reactiveX intro link provided by Robert Harvey apply.
Conceptually promises are a subset of observables. Promise is a value that will resolve asynchronously. Most typical example is http requests. Observables deal with sequence of asynchronous events. These events could be mouse positions, clicks, user input etc. So one could say observables are richer and more complex abstraction for handling asynchronicity.
So, calling observables as promises + helper methods is an oversimplification. The answer to your question depends on whether you need to deal with sequence of events or if you can live with "simple" asynchronicity. Libraries like RxJS or Bacon or Kefir add complexity so you'll want to add them only if needed.
- "calling observables as promises + helper methods is an oversimplification" – +1, that's like calling JavaScript "C with hashmap literals".Jörg W Mittag– Jörg W Mittag2016-05-23 11:12:58 +00:00Commented May 23, 2016 at 11:12