Skip to content

Latest commit

 

History

History
33 lines (22 loc) · 712 Bytes

observable.md

File metadata and controls

33 lines (22 loc) · 712 Bytes

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.

import { from } from 'rxjs';

let stream$ = from([1,2,3])

stream$.subscribe( (value) => {
   console.log('Value',value);
})

// 1,2,3

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

let promise = new Promise((resolve, reject) => {
   setTimeout(()=> {
      resolve( [1,2,3] )
   })

})

promise.then((value) => {
  console.log('Value',data)
})

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