Observable vs Promise

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris

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
1
2
3
4
5
6
7
8
9

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)
})
1
2
3
4
5
6
7
8
9
10

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