Observable vs Promise

Let’s dive right in. We have created something called an Observable. An async construct, much like a promise that we can listen to once the data arrives.

  1. let stream$ = Rx.Observable.from([1,2,3])
  2. stream$.subscribe( (value) => {
  3. console.log('Value',value);
  4. })
  5. // 1,2,3

The corresponding way of doing this if dealing with promises would be to write

  1. let promise = new Promise((resolve, reject) => {
  2. setTimeout(()=> {
  3. resolve( [1,2,3] )
  4. })
  5. })
  6. promise.then((value) => {
  7. console.log('Value',data)
  8. })

Promises lack the ability to generate more than one value, ability to retry and it doesn’t really play well with other async concepts.