4

I'm currently reading about observables and promises. From what I understand promise only returns a single value whereas observable can return a stream of values. I understand the concept of of observables in simple cases like following

const myObservable = of(1, 2, 3);

// Create observer object
const myObserver = {
  next: x => console.log('Observer got a next value: ' + x),
  error: err => console.error('Observer got an error: ' + err),
  complete: () => console.log('Observer got a complete notification'),
};

but why use observables in angular http calls? Since server responds to http calls only once ,it returns a single value not a stream of values right?Then what is the advantage of using observables over promises in http calls?

Arjun
  • 1,116
  • 3
  • 24
  • 44
  • 2
    https://github.com/angular/angular/issues/5876#issuecomment-164447013 – maxime1992 Feb 04 '20 at 09:08
  • 4
    Because Angular uses RxJs everywhere, so it only makes sense to use them there as well, so you don't have to convert back and forth. Also RxJs adds so much to async calls, it's really powerful. Canceling requests, throttling calls, delaying calls, repeating calls and so on. – Roberto Zvjerković Feb 04 '20 at 09:09
  • 1
    Several advantages: you can cancel a subscription, cancelling the request, retrying a request if needed is as easy as using the `retry` operator in the pipe... and there's about a hundred potentially useful operators more. – mbojko Feb 04 '20 at 09:14
  • I have to agree with observables being more usefull overall. Considering the main place where people encounter Observables is in http calls, I would read the answers provided here: https://stackoverflow.com/questions/37364973/what-is-the-difference-between-promises-and-observables They go in depth and give multiple examples as to why Observables are generally better. – Bjorn Pijpops Feb 04 '20 at 09:23
  • Also think about the `keep-alive` header, this could potentially generate more responses from the http call. Or when you use the `reportProgress` property and observe `events`, you'll get a stream of upload or download progress – Poul Kruijt Feb 04 '20 at 09:29
  • @ritaj is right. And `http` call automatically close subscriber when response is received – Dumbo Feb 04 '20 at 09:46
  • Possible Duplicate of : https://stackoverflow.com/questions/41600456/why-does-the-http-service-return-observables-instead-of-promises?rq=1 – Nah Aug 07 '22 at 16:09

1 Answers1

0

Observables are way more powerful. We can easily create code that "retries" http calls, in case the first one failed an similar things. You can't do this with promises at all. In addition, another thing you can't do with promises is cancelling calls. Just think of the use case where you build a type ahead component and with each keystroke you have to throttle 300 ms before you send the actual Api Call. This is very easy with observables, but not only that, with observables we can make sure that we always execute the return call in the right order + we can cancel calls.

user1034912
  • 2,153
  • 7
  • 38
  • 60