Is there any case, where Promise is more powerful as compare to observable? I know a lot of benefits of observables over promises. But Is there any case, I should use only promises over observables.
I found this link, promises vs observables. But this always shows me the benefits of observables over promises. I want to know the benefits of promise over observables.
An observable does everything that a promise does and more. It can always be switched to a promise in case one is expected with toPromise()
method (firstValueFrom
and lastValueFrom
in RxJS 7).
An observable must be chosen over a promise if
Observable.from(...)
safety structure to unify observables and promisesAn observable may be chosen over a promise if the code where it's used uses observables exclusively.
A promise must be chosen over an observable if API that consumes it expects a promise and doesn't use Observable.from(...)
safety structure.
A promise may be chosen over an observable if
async
functions)let observable = ...; observable.subscribe(...); return observable
(this also requires multiple subscriptions to be tracked in case an observable is cancellable)