Difference between Promises vs Observables

Observable can provide a stream of data, also can filter the results and you can unsubscribe. It is not possible to do with Promises.

Observable can return a value synchronously or asynchronously. Promise is always asynchronous


const myPromise = new Promise(resolve => {

  setTimeout(() => {

    resolve('dog');

  }, 100);

});



myPromise.then(result => {

  console.log('promise: ', result);

})



const myObservable = new Rx.Observable(observer => {

  setTimeout(() => {

    observer.next('dog');

    observer.next('cat');

    observer.next('bird');

  }, 100);

});



const subscription = myObservable

  // .filter(result => result === 'bird')

  .subscribe(result => {

    console.log('observable: ', result);

  });



// subscription.unsubscribe();

Difference between Promises vs Observables

Leave a Comment