Difference between Promise and Observable?

Home >> Blog >> Difference between Promise and Observable?

Difference between Promise and Observable?

The both are used for handling asynchronous operations.

Key Difference - Promise emits a single value while Observable emits multiple values.
When to use Promise and when to use Observable?

Suppose, while handling a HTTP request, Promise can manage a single response for the same request, but what if there are multiple responses to the same request, then we have to use Observable.

Becasue, Observable can handle multiple responses for the same request.

Note - Observable can be converted into Promise using toPromise() method

Observables are a part of the RXJS library.
let promise = new Promise((resolve, reject) => { resolve(1); resolve(2); resolve(3); }) .then(result => console.log(result)); //Output 1
import {Observable} from 'rxjs'; let observable = new Observable((observer) => { observer.next(1); observer.next(2); observer.next(3); }).subscribe(result => console.log(result)); //Output 1 2 3
Sr. No. Observables Promises
1. Emit multiple values over a period of time. Emit a single value at a time.
2. It's lazy: only executed when we subscribe using subscribe() method. It's not lazy: execute immediately after creation.
3. Are cancellable using the unsubscribe() method Are not cancellable.