Creating Observables

There are several ways to create Observables, the create operator being the most obvious one. The create operator in the Rx.Observable object takes a callback that accepts an Observer as a parameter. That function defines how the Observable will emit values. Here’s how we create a simple Observable:

 const​ observable = Observable.create(observer => {
  observer.next(​"Simon"​);
  observer.next(​"Jen"​);
  observer.next(​"Sergi"​);
  observer.complete(); ​// We are done
 });

When we subscribe to this Observable, it emits three strings by calling the next method on its listeners. It then calls complete to signal that the sequence is finished. But how exactly do we subscribe to an Observable? We use Observers.

The Observer ...

Get Reactive Programming with RxJS 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.