Benefits of Observable with Http in Angular
Asked Answered
D

6

8

From here: https://mcmap.net/q/45060/-what-is-the-difference-between-promises-and-observables

First this answer describes how Observables are helpful in preventing same repeated requests to the server and how we can pause between many requests so that the server doesn't get overloaded.

They it says:

As far as I am using Http in Angular, I agree that in the normal use cases there is not much difference when using Observable over Promise. None of the advantages are really relevant here in practice. Hope I can see some advanced use case in the future :)

What I understand here is that when Http is used then Observables benefits are not really relevant.

Why is that so? What role is played by Http in this case?
What topic do I have to study to understand Http's role here?

Dornick answered 18/8, 2020 at 5:57 Comment(7)
Well, if you restrict the area to the HTTP you may be right. But in a real-world scenario, you will eventually need denounces, pollings, event filtering, event merging, combine component events with HTTP services, etc. then you can taste the sweetness of the observables.Fresh
if you has an Observable, you can use Rxjs to mannage. This allow you use operators as debounce,distinct,delay,pairwise,throttle,timeout,share...:learnrxjs.io/learn-rxjs/operators/complete. For me there are a great difference between a Promise and an ObservableBuhrstone
@Buhrstone Please read the question again. I want to understand what has Htttp to do with Observables effectiveness. This quote in OP As far as I am using Http in Angular, I agree that in the normal use cases there is not much difference when using Observable over Promise. Please explain what are talking about http.Dornick
@Buhrstone I want to understand what has Htttp to do with Observables effectiveness. This quote in OP As far as I am using Http in Angular, I agree that in the normal use cases there is not much difference when using Observable over Promise. Please explain what are talking about http.Dornick
Imagine you make a call to an API that return a list of "object". You want to make one call for each object to get a list of "object with properties". using switchMap and forkJoin you subscribe and get in a response the list. As Angular is made with observables. the FormControl.changeValues is an observable, again you can use switchMap to "convert" the change of the input in calls to an API that give you a result and you can filter before make the call. Another, you want to make a "loading". you can create an operator to show it. Using Share you can avoid make the same call more that one time.Buhrstone
I understand that Observables has too much operators, but really, at first with switchmap, takeWhile and forkjoin is enougth in the most of cases to call an APIBuhrstone
Maybe this can help #49885835Wycoff
R
6

The observable that HttpClient returns usually only emits one value then completes which, on the surface seems a lot like a promise. However, the idea that it is purely for consistency and/or that promises are always eschewed in Angular isn't true. As others have noted, the async pipe supports promises as does application init. So why use observables instead of promises for http requests? Because observables offer another layer of abstraction.

In that thread you linked, this comment makes a crucial point:

@gman Exactly. The Promise simply represents some future value. It does not represent the operation which generates the value. You cannot cancel a value. You cannot retry a value. It's just a value. It may or may not be present yet, and it may never exist because an exception occurred, but that's it.

Cancellation

Let's talk about cancelling a HTTP request. In the case of the observable, HttpXhrBackend is just one implementation of HttpBackend and handles the cancellation of the observable by calling XMLHttpRequest.abort():

// This is the return from the Observable function, which is the
// request cancellation handler.
return () => {
  // On a cancellation, remove all registered event listeners.
  xhr.removeEventListener('error', onError);
  xhr.removeEventListener('load', onLoad);
  if (req.reportProgress) {
    xhr.removeEventListener('progress', onDownProgress);
    if (reqBody !== null && xhr.upload) {
      xhr.upload.removeEventListener('progress', onUpProgress);
    }
  }

  // Finally, abort the in-flight request.
  if (xhr.readyState !== xhr.DONE) {
    xhr.abort();
  }
};

Note that when you're consuming this promise, you don't actually care that it's using XMLHttpRequest and it could be using SomeWhackyAngularXMLHttpRequestThatIsBetter. Let's say we return a promise instead:

// How would something that consumes this call xhr.abort()?
function myHttpGetPromise(method, url) {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.onload = resolve;
        xhr.onerror = reject;
        xhr.send();
    });
}

How could your client cancel the request with just that promise? You would have to either:

  1. Somehow expose the implementation (our XMLHttpRequest instance in this case).
  2. Provide your own layer of abstraction around the XMLHttpRequest (something like httpPromise which supports abort) to allow for cancellation.

Re-use

Promises are not re-usable. Let's say you want to retry an HTTP request using promises. How would you do it? You guessed it: you would have to add another layer of abstraction. In the case of observables, we have retry support out of the box.

Conclusion

One thing worth mentioning in closing is that the angular HttpClient doesn't always just return one value. In the case where you set reportProgress to true, it emits multiple HttpEvents before finally completing when the request is complete. See the docs for more info. And finally, you should read the original issue where this was debated in the Angular repo for some backstory.

Razzledazzle answered 27/8, 2020 at 0:17 Comment(0)
E
3

Angular is built upon RxJs. Async functions like the EventEmitter are RxJs observables, forms. RxJs is great for asynchronous functionality. Seeing that Angular is built upon RxJs when they are building a service that provides http functionality the devs are going to choose RxJs. When we build an asynchronous app with RxJs we don't mix it with promises.

It might not look like there is much benefit when you just think about sending a request and getting a response but once you start working with RxJs and understand all the RX operators it clicks.

Learning RxJs before you learn Angular will make you a much better Angular dev. I wish somebody told me this a few years ago because the apps I wrote in the early days where I didn't quite fully grok RxJs would have been written quite differently if I spent some time learning the RX functions.

The async pipe is also a another great tool if you are using observables, it does work with promises as well, but it means you can use observables directly in your templates with having to manage subscriptions in the code.

Emotion answered 20/8, 2020 at 7:19 Comment(3)
Please read the question again. I want to understand what has Htttp to do with Observables effectiveness. This quote in OP As far as I am using Http in Angular, I agree that in the normal use cases there is not much difference when using Observable over Promise. Please explain what are talking about http.Dornick
Learning RxJs before you learn Angular is a 🚀🚀 advice 👍👍Pitchford
BTW, Learning RxJs before you learn Angular will make you a much better Angular dev. I have taken your advice seriously and have started learning RxJs. Thank you.Dornick
C
2

First of all, it's about consistency. That's not a good idea to mix Promise and Observable, you should always remember what and where you have used.

Also Observable add some extra features that missed in Promises. Let's leave all RxJs operators that help you manipulate data out of this post's scope.

There are three main points of Observable usage, that give more benefit rather than Promise:

  • Retries that are not doable by Promise.
  • Throttling and debouncing can be implemented in one line.
  • Cancelation that is not supported by vanilla Promise,
Constellate answered 25/8, 2020 at 17:8 Comment(0)
U
1

The first thing that should be mentioned is that it is an opinion. As you can read in the answer comment, some people agree and some people disagree.

There is only one thing I can think of that makes Http kinda special??. Http Observables are always cold. That means you get only one result or an error. That makes Http observables kinda easy. You probably want to call the endpoint maybe modify the data a little and show it on the screen.

Other Observerables in Angular such as the form.valueChanges() or the routerState are hot. That means multiple results MAY come back. I can imagine you would do more complex things with that.

Ultranationalism answered 23/8, 2020 at 22:0 Comment(2)
Cold doesn't mean you only get one result, cold means there is no activity if there are no subscribers. Http observables complete once they emit, that is why there is only one result. Has nothing to do with hot/cold.Emotion
medium.com/@luukgruijs/…Emotion
S
1

What makes an http call sort of a special case for Observables is that an http call can be seen as a stream which either emits just one value and then completes or it errors.

Observable streams in general can emit more than one value, may or may not complete, may or may not error.

So for this reason, i.e. at most one value emitted, http Observables are close to Promises, which can actually emit just one value or error.

Nonetheless there are advantages in using Observables for http calls in real life scenarios, specifically when you have to compose multiple http calls with other async streams of events. In this article there are some typical patterns of use of Observables with http calls where you can appreciate the benefits of rxjs operators.

Snuffer answered 26/8, 2020 at 7:53 Comment(0)
V
0

The key difference in my opinion is how Observables has the privilege of being able to cancel subscriptions. Promises is relying on having either to Resolve or Reject. There is so many other good advantages of using Observables too, and I primarily tend to Angular. It's a dreamland.

Vexatious answered 23/8, 2020 at 15:36 Comment(9)
Please read the question again. I want to understand what has Htttp to do with Observables effectiveness. This quote in OP As far as I am using Http in Angular, I agree that in the normal use cases there is not much difference when using Observable over Promise. Please explain what are talking about http.Dornick
Your question is not properly described, what would you like to know? If you need to know the role of Observables in angular, or the http part - go study this site angular.io/guide/observables-in-angularVexatious
I have highlighted the quote in the above comment. I want to understand its meaning.Dornick
The thing you quoted, is not true. There is many use cases where you would priorities observables instead of promises. (Please note you referring to a 4 years old post)Vexatious
As far as I am using Http in Angular I only want to understand why are they talking about http here.Dornick
@Dornick What do you mean?? It costs efficiency not to handle http without Observables, in the form of making multiple calls.. You have not defined what it is your question is related to. I only want to understand why they talking about http here. - you don't precise what you want to know.Vexatious
Oh, It costs efficiency not to handle http without Observables, in the form of making multiple calls.. I have didn't know anything of this sort. I am new new to all this. Could you tell me what should I read to know how http and observables are related or helpful to each other.Dornick
In fact, if you add this to your answer, it may become more appropriate and complete.Dornick
Observables in the settlement should be as efficient as Promises, if the sake is just for returning data. If you want a real-time effect invoked on your application, you would choose the other paradigm of returning data. But get me right, it's not in the way that a Promise is resolving before or AOT compared to a Observable's subscription. So if it is for basic usage, if you tend to Promises - performancewise there's no reason for switching from using Promises. It's not like Observables get the data before the Promise.Vexatious

© 2022 - 2024 — McMap. All rights reserved.