I made a spring reactive backend which returns Flux<> object. When I call the service with angular httpClient and subscribe to the observable returned. The result is never triggered. So why Observable is returned rather than a Promise if only one response is possible ? I have tried the same with hardcoded nodeJS SSE service. I have the same result, but it work well with an event source. My question might be weird but the Observable wasn't choose by chance, so my understanding is certainly wrong and I want to understand.
Asked
Active
Viewed 2,114 times
5
-
1Observables work perfectly fine with a single response. Why shouldn't it? – John Montgomery May 02 '19 at 19:56
-
My question is unclear sorry. I want to know how Observable if it can be triggered only one time ? It add complexity for nothing so. – Pred05 May 02 '19 at 19:58
-
2An observable that emits a single value and completes is no more complex than a promise. – John Montgomery May 02 '19 at 20:02
-
With a promise you didn’t have to unsubscribe, only one response is expected. For me Observable is like a queue. It is used for send message and doing something when one is sent. – Pred05 May 02 '19 at 20:11
-
2You don't have to unsubscribe from an observable that completes, either. – John Montgomery May 02 '19 at 20:12
-
@JohnMontgomery the problem is that you don't know **when** the Observable will complete. It might complete **after** the component is already destroyed. Imagine you're on a very slow network and the component invokes the HttpClient which completes over 10s. If the user navigates away in 5s, the component is destroyed but still has an active Observable subscription: it wasn't unsubscribed! The destroyed component will still process the Observable, is that the behavior you would expect in your app? See https://stackoverflow.com/questions/35042929/ – Wouter van Koppen Aug 17 '20 at 05:39
-
1@WoutervanKoppen Promises keep running if the component is destroyed too, so that still isn't meaningfully different as far as this question is concerned. If anything that's another point in favor of the observable since they have more options for handling cases like that. – John Montgomery Aug 17 '20 at 17:16
-
1I agree it should be Promise since the type statically guarantees a single value. Promises are strictly simpler. You can easily "upgrade" to Observable using `defer`. The advantages of Observable are not always applicable, so why force the added complexity? The situation is more unfortunate now that `toPromise` is deprecated. – cambunctious Apr 14 '22 at 19:06
-
@cambunctious I share your opinion. Observable is often, for me also, a useless complexity. Even after 3 years I posted the question, I'm not convinced by the necessity of RxJS. – Pred05 Apr 26 '22 at 14:40
-
After a second look, `Observable` is in fact necessary to support `reportProgress`, which involves multiple events. Though I still think it's unfortunate to not support `Promise` for the common case. But then there is `firstValueFrom()`. – cambunctious Apr 26 '22 at 22:54
2 Answers
14
Observables have several advantages compared to promises when making HTTP Requests:
- Retry failed requests (
retry
,retryWhen
) - Cancel unnecessary/stale requests (
switchMap
,unsubscribe
...) - Better error handling
- Easily combine multiple HTTP calls, and have control of how to execute them (eg. one by one, or in parallel).

Alberto Rivera
- 3,652
- 3
- 19
- 33
-
1Care to add/elaborate on how is error handling better with Observables? – Andrej K Nov 09 '22 at 03:46
2
Angular httpclient will always return an observable (RXjs) , promise is not available in Angular, it was available in AngularJs, you need to subscribe to the observable
for more info read documentation to read more about Observables check this if you are new to Rxjs check this

dota2pro
- 7,220
- 7
- 44
- 79
-
I know that. But why an Observable if the Observable can be triggered only one time ? – Pred05 May 02 '19 at 19:56
-
@Pred05 I suggest you read up on how rxjs observable pattern actually works. It can be sligthly steep learning curve but It's worth it :) – Lucho May 02 '19 at 19:58
-
-
@Pred05 subscribing to observable is like subscribing to magazine it will keep on looking on for changes promise always returns success of reject, subscribing to observable means keep looking for changes to the observable makes sense ? edited my post too – dota2pro May 02 '19 at 19:59
-
2"promise is deprecated in new Angular versions" - this doesn't make sense. Promise is not provided by Angular. – cambunctious Apr 14 '22 at 19:11
-
-
1@dota2pro Promise is a JS type, not provided by any library. Both Angular and AngularJS have API's that use Promise, like AsyncPipe. – cambunctious Apr 15 '22 at 13:45