1

We don't really (standard REST apis to my knowledge) keep looking for values of the response over a period of time, unless the response is huge.

Can somebody guide me as (why?) a Http response is modeled as Observable?

What am I missing?

Divs
  • 1,578
  • 2
  • 24
  • 51
  • 1
    I guess the main reason for Http was that it can be cancelled. – Günter Zöchbauer Jun 20 '16 at 07:56
  • @GünterZöchbauer - Ok, Nice. But that binds us to make use of Observable paradigm and even though we are looking at the response data for a single call, we need to "subscribe" to that response to get that data. Now, we may not want the data to be displayed in the component html template and may want it to compute something, which leads us with no other choice than subscribing it over a sync call <>.. and thereby also have to put a unsubscribe code, which makes it a little boilerplate.. or is there any other way of doing this?? – Divs Jun 20 '16 at 08:15
  • 1
    You don't need to unsubscribe. When `Http` closes the `Observable` because the response from the server was arrived, your subscription is unsubscribed automatically. `Observable` is lazy, therefore you need to add a `.subscribe()` or `.toPromise()` to make it execute the call but it also allows to use it in both scenarios (with promises and with observables). I don't know what you mean by "Now, we may not want the .... subscribing it over a sync call <>.". You have just `subscribe(...)` instead of `then(...)`. – Günter Zöchbauer Jun 20 '16 at 08:21
  • 1
    Thanks. I have confused 2 topics here. I still have some doubts of using the Http Response with ngrx / store. But will have separate post for that. – Divs Jun 20 '16 at 10:20
  • @Divs: I was asking the same question before - the question Gunter linked provides a good answer (he knows his stuff). With that said, the short answer is that there is NOT really a good reason to use Observables over Promises in practice when it comes to http: "Oh it's retriable, oh it supports debounce, oh it's cancellable" are all pretty irrelevant to this use case. – VSO Jan 30 '17 at 16:01

0 Answers0