Observer
- An Observer is a consumer of values delivered by an Observable.
- An Observer’s job is to perform a certain action(s) whenever a value comes from the data stream.
- New data may not always be a value it may be an error or a signal that observable is complete(done emitting values).
- Hence, we need to connect the observer and the observable. This can be done using
Subscription
. - With
subscribe()
method we tell the observable that someone is listening out for these values. - Observers can implement three methods:
- next(): called whenever a new value is emitted
- error(): called whenever an observable throws an error
- complete(): called whenever an observable is done
- Some observables may never finish (click listener).
RxJS doesn’t deliver notification after error() or complete() is called. This means emitting next value after error() or complete() is not allowed.