Search code examples
angularrxjsobservableangular7

Why does Angular httpClient returns an Observable instead of a Promise?


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.


Solution

  • 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).