11

I'm curious about the reasoning or discussion that lead to the angular 2 http service returning an observable instead of a promise for the response. I would understand the logic for something like a web socket connection or long-polling request, but the http service simply creates an XMLHttpRequest, and in the load event handler for the request, emits the response on the response observable and then completes the observable. Since this only returns one value, it seems to me like a promise would make more sense.

Edit: I am aware of the difference between observables and promises, and know that you can convert observables to promises and vice versa. My question is more about the reasoning behind the decision to return an observable instead of a promise.

bringel
  • 482
  • 5
  • 17
  • Possible duplicate of [Angular 2 - promise vs observable](http://stackoverflow.com/questions/37364973/angular-2-promise-vs-observable) – Fals Jan 11 '17 at 20:54
  • 1
    You can fall back to promises https://angular.io/docs/ts/latest/guide/server-communication.html#!#promises Just put the .toPromise() method after your call – R_Ice Jan 11 '17 at 20:56
  • The question is why it isn't declared as a Promise or a Future if it behaves like one. – Florian F Jun 10 '19 at 20:58

1 Answers1

5

I think the main reason was that observables can be cancelled.

See also Angular - Promise vs Observable.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567