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:
| var observable = Rx.Observable.create(function(observer) { |
| observer.onNext('Simon'); |
| observer.onNext('Jen'); |
| observer.onNext('Sergi'); |
| observer.onCompleted(); // We are done |
| }); |
When we subscribe to this Observable, it emits three strings by calling the onNext method on its listeners. It then calls onCompleted to signal that the sequence is finished. But how exactly do we subscribe to an Observable? We ...
Get Reactive Programming with RxJS 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.