Observables

Observables are a new way of approaching flow control, whereby you subscribe to a data source and then run functions based on events. While you can use them for anything, they're best suited to situations where the data does a lot of updating - for instance, in live stock market charts. We can also use Observables to run updates based on push events, such as when connected to a WebSocket stream, which reduces the need to constantly poll a single endpoint.

The most common library for working with Observables right now is RxJS. Whereas, with Promises, we'd do something like:

const data = await (await fetch(url)).json()

With RxJS, we would instead do something like the following:

  Rx.Observable 
.fromPromise(fetch('/data/cultural.json'))
.flatMap(v => v.json())
.subscribe(
(data) => {
console.dir(data);
},
(error) => {
console.dir(error);
},
() => {
console.log('complete!');
}
);

There's no way we can possibly cover anywhere near the level of depth necessary to adequately learn anything useful about Observables here, they're a really complex topic best suited to when one needs to respond to live data. For now, if your interest has been piqued, try taking a look at the RxJS book, available free online at https://xgrommx.github.io/rx-book/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset