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 promi
Observable is lazy --> nothing will happen until we subscribed the observable. Observable can be cancelled at anytime by unsubscribing it for memory clean up. Promise can have only one response but observable can have more than one responses.
const promSingle = new Promise(resolve){
resolve('a');
resolve('b');
}
promSingle.then(value => {
console.log(value)
})
Print only a In Observable :
const obSerMult = new Observable(observable){
Observable.next('a');
Observable.next('b');
Observable.next('c');
Observable.next('d');
}
obSerMult.subscribe(value => {
console.log(value);
})
print will be a, b , c ,d
From: Randall Koutnik book “Build Reactive Websites with RxJS.” :
Observables are like arrays in that they represent a collection of events, but are also like promises in that they’re asynchronous: each event in the collection arrives at some indeterminate point in the future. This is distinct from a collection of promises (like Promise.all) in that an observable can handle an arbitrary number of events, and a promise can only track one thing. An observable can be used to model clicks of a button. It represents all the clicks that will happen over the lifetime of the application, but the clicks will happen at some point in the future that we can’t predict.
From: Anton Moiseev Book “Angular Development with Typescript, Second Edition.” :
Promise has the following shortcomings:
There’s no way to cancel a pending request made with a promise.
When a promise resolves or rejects, the client receives either data or an error message, but in both cases it’ll be a single piece of data. A JavaScript promise doesn’t offer a way to handle a continuous stream of data chunks delivered over time.
Observables don’t have these shortcomings.
Promises are eager, hence use them where you want something to happen immediately without any trigger. whereas Observable are lazy, which can be used to do something that requires some triggers either from input or something else and does not need to be happen immediately the app is loads
Use Promise instead of an Observable, when:
Use Observable instead of a Promise, when:
Generally, the Observable pattern is an extended Promise pattern, with a lot more tools and functionality. It is up to you to decide to limit the code with Promises or not. It was first a custom libary, then got included in ES2016.
Also, I suggest researching the issue with specific problem parameters: you need the app to be faster? You will use legacy modules?
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
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)