Brief
An observable does everything that a promise does and more. It can always be switched to a promise with
toPromise()
method in case a promise is expected.
An observable must be chosen over a promise if
- any feature that are intrinsic to observables and not promises and explained in detail in related question is in demand (notably incomplete observables and observables that receive multiple values)
- API that consumes it expects an observable and doesn't use
Observable.from(...)
safety structure to unify observables and promises
An observable may be chosen over a promise if the code where it's used uses observables exclusively.
A promise must can be chosen over an observable if API that consumes it expects a promise and doesn't use
Observable.from(...)
safety structure.
A promise may can be chosen over an observable if
- the code where it's used uses promises exclusively (notably
async
functions) - it needs to be immediately subscribed and chained then, because a chain should be broken in observables
let observable = ...; observable.subscribe(...); return observable
(this also requires multiple subscriptions to be tracked in case an observable is cancellable)
Comments
Post a Comment