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:
map
, reduce
, retry
and more.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.
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.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?
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:
Observable.just
are fully synchronous.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With