2

I have been doing javascript for a while now. However, I am fairly new to Angular 2, so I know almost nothing about Observables. When I was reading the descriptions of Observables they sounded a lot like the callbacks that I already knew. When I asked Google there were comparisons between callbacks and promises, and there were comparisons between promises and observables. However, I was unable to find any comparison between callback and Observables.

What is the difference between callbacks and observables?

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
  • You need to restudy the difference between callbacks and promises, then this question will be obsolete. You can compare promise and observables, but the callbacks are different concept. You register callbacks to promises, observables or whatever you want. – s.alem Jul 21 '17 at 23:39
  • 2
    callbacks, ie. having the ability to pass function references, is the key concept that enables promises and observables. So it's not Callbacks versus Observables, but callbacks AND observables – Joel Jul 22 '17 at 00:06

1 Answers1

1

As discussed in this thread, the main difference between Observables and Promises are that Promises handle one-time asynchronous events that can either resolve or fail, while Observables enable composable streams through various operations attached to an asynchronous streaming data or event source. In the end, in order to pass the results from both an Observable or a Promise back to synchronous code, you will need a callback to operate as a data sink, so the two are not mutually exclusive. That being said, Observables, like Promises, allow for the syntactic sugar of using composable functions to declaratively define operations on an asynchronous data stream. In the case of Observables, you can use a single callback as a sink to pass the results back to synchronous code rather than having to nest function calls and/or performing recursive function calls like would be the case in composing asynchronous operations using pure callbacks.

Jason
  • 31,834
  • 7
  • 59
  • 78