In this lecture, we are going to explore the other functions we can add to an observer.
The job of the observer is to read values as they are pushed in our script file.
We're calling these subscribe function for registering a new observer.
The object we're passing in can have up to three functions one function for handling emitted values,
another function for handling errors, and lastly a function for handling the completion of an observable.
The next function we're passing in will handle values pushed from the observable.
We can push multiple values over time from an observable.
Hence, why it's sometimes referred to as a stream of data.
For example, inside the observable, we will call the next function with a different message.
Inside the console, two separate logs are made, the function we passed into the subscribe function
is being called twice.
That's important to understand.
Observables do not push data all at once.
This behavior allows us to push data over time.
At the moment, we have a memory leak issue.
Observables are constantly running.
They are not automatically terminated.
Our SJS has a couple of functions for performing this task, which we will explore later.
For now, let's learn how to manually terminate an observable inside the observable.
We are going to call the complete function on the subscriber object.
The complete function will prevent the observable from pushing new data, for example, nothing would
happen if we attempted to call the next function after calling the complete function.
As we can see in the console, the message in the third next function does not get logged inside the
observer.
We can run a function on completion.
Let's define a method called complete.
By calling the complete function, observers will be notified of the observables completion.
This function isn't passed a value.
Let's log a message to help us verify if the observable is complete.
Inside the console, the message has been logged from our observer.
There's one less function we are going to add to our observer.
We can handle errors by adding the error function.
This function will have one argument, which is called error.
The error argument will store the error produced by the observable, we will log the error with the
console port error function.
Unlike the log function, the error function will produce a flaw with a red background after making
those changes.
Let's update our observable to throw an error.
We will call the subscriber error function after the second next function with a basic error message.
Inside the console, the air gets logged with our message.
However, the message from our complete function never gets logged.
If an error is thrown from an observable orange juice will terminate the observable.
We won't be able to emit new values or complete the observable.
Always keep this in mind when working observables other than calling the complete function.
Errors can terminate an observable.
There's one less thing I want to mention.
Observers can be partial.
We don't have to define all three functions in our observer.
They are completely optional if we forget to define the other functions.
Our SJS will not complain.
It will run dummy functions for handling these events, which won't do anything before moving on to
the next lecture.
I'm going to completely empty out the observable.
In the next lecture, we will look at how to handle asynchronous operations from within and observable.