Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Should we use Observables? or Promises are enough? [closed]

I'm long familear with JavaScript's Promises. I know that today Promises are part of the JavaScript language - since ES6, but long before that they there (and still are) a few different libraries that implemented them.

I've started working recently on a few Angular projects and I was introduced to the concept of Observables (by RxJs).
After some invastegations I understand the basic difference between Observables and Promises:

Observables

  • Passing 0 to N events. Each event can invoke a callback.
  • An observable is Cancelable
  • They are a lot of different methods (Implemented by RxJs team) that can help me read and parse the data like: map, reduce, retry and more.
  • Observables block will be fired only if someone had subscribe to them (otherwise nothing will happen)

Promises

  • Handle a single event, that will invoke a success callback or a failure callback.
  • The ES6 Promises aren't cancelable (for now at least). However different libreries have implemented cancelable promises e.g. BlueBird.
  • Promises block will be fired whether someone had subscribe (using then or catch) to them or not.

My question is not what is the difference between them, but do we actual need Observables, or are they just syntactic sugar? Since the use of Promises is to sync (set in order) an async flow, by telling one block of code to run only when anther block of code had finished it's flow.

  1. Do we really care about handling multiply events, since almost always we would like to react on success or a failure of some flow (like a server request).
  2. The RxJs methods (helper functions) for handling Observables are cool, but not really relevant since you can use a 3rd party library for that behavior (for example instead of using RxJs debounce I can use Lodash _.debounce with a Promise). Of course, each 3rd party library comes with it's additional complexity - but so does RxJs.
  3. Observables being fired only after subscription - this isn't so important - why would we want to have an Observable if nobody is listening (subscribe) to them.

P.S
Please don't read this question and think I have something personal agents Observables, I just want to understand where they shiny the most? and in which cases they are superior then Promises, if ever?

like image 336
Gil Epshtain Avatar asked Oct 16 '22 18:10

Gil Epshtain


1 Answers

I do believe that instead of asking do we need observables? we should ask when do we need observables? There are some things that you missed as differences:

  1. Observables are not always asynchronous, some operator like Observable.just are fully synchronous.
  2. There are hot and cold observables, which means some observables only start emitting after you have subscribed to them, HttpClient::get is one of them. While promises evaluate eagerly.

RXJS Observables aim to provide an easy way to do functional reactive programming, while promises are only a way to do asynchronous programming.

Based on that I can say that Observables are not just syntactical sugar for promises, but rather a completely different structure.

like image 172
J. Pichardo Avatar answered Nov 02 '22 07:11

J. Pichardo