Friday, March 13, 2020

Promise Vs Obserable

Promises: 

Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations.

const promiseTest = new Promise((resolve, reject) => {
resolve('Its Resolved.');
});

promiseTest.then(res => console.log(`Message from: ${res}`));

Observables: 

Observables provide support for passing messages between publishers and subscribers in your application. Observables are declarative—that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it.

const { Observable } = rxjs;

const obserableTest = new Observable(observer => {
observer.next('I am observable');
observer.complete();
});

Difference between Observable Vs Promise:

 1. Observable can emit multiple values whereas Promise can emit only single value.
 2. Observable is cancelable while Promise is not.
 3. Observable can be asynchronous/synchornous while Promise can only be asynchronous.
 4: Rxjs operators can be applied on Observable since it is like stream while not possible on Promise.



No comments:

Post a Comment

Promise Vs Obserable

Promises:  Promises  are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynch...