To listen to data on an observable, we must call the subscribe()
method. This method returns a subscription, which we can use later to stop reacting to the incoming data if we are no longer interested in it.
The subscribe()
method of observables has the following signature:
observable.subscribe(onNext,onError,onCompleted);
All parameters are optional and can be omitted if we are not interested in this type of event:
onNext
: This is a function to be called every time new data is propagated through the observableonError
: This is a function to be called every time an error occurs in the observableonCompleted
: This is a function to be called when the observable is completed
The easiest way to subscribe to an observable is to just pass the onNext
parameter (as we have been doing in most of our code snippets in this chapter):
Rx.Observable .just('Hello World!!!') .subscribe((message)=>console.log(message));
If you run the preceding code, it will print...