What is observable, observer and subscribe in angular?

Here is a simple visual to see the difference:

enter image description here

As seen above … an Observable is a stream of events or data. They are often returned from Angular methods, such as the http.get and the myinputBox.valueChanges.

Subscribing “kicks off” the observable stream. Without a subscribe (or an async pipe) the stream won’t start emitting values. It’s similar to subscribing to a newspaper or magazine … you won’t start getting them until you subscribe.

The subscribe method takes in an observer. An observer has three methods:

  • The method to process each time an item is emitted from the observable.

  • The method to process any error that occurs.

  • The method to clean up anything when the observer completes. This last one is seldom used when working with Angular’s observables.

(And I agree … trying to see the forest through the trees of the docs is quite a challenge. I understand they are working to improve them.)

Leave a Comment