Lesson 33. Observables

 

After reading lesson 33, you will be able to

  • Create your own observables
  • Subscribe to observables
  • Compose new observables with higher-order combinator functions
  • Create your own combinator functions for composing observables

Observables are objects in which you can subscribe to streams of data. Observables are like promises, but whereas a promise only resolves or rejects once, an observable can keep emitting new values indefinitely. If you were to think of promises as an asynchronous datum that you can wrap around setTimeout, observables would be the asynchronous data that you can wrap around setInterval.

Note

In order to use observables today, at the time of this writing, you need to use one of the open source implementations. Currently zen-observable is the closest implementation of the spec: https://github.com/zenparsing/zen-observable.

Consider this

Websockets allow the front end to subscribe to events from the back end. With WebSockets, the server can push new values to the client as they become available. Without them, you would need the client to pull new items from the back end by continuously polling the server, asking if new data is available yet. With WebSockets, you could say that the client is observing from the server. Wouldn’t it be nice to be able to use this same push mechanism of sending new data to any observer?

33.1. Creating observables

33.2. Composing observables

33.3. Creating observable combinators

Summary