In this lecture, we are going to look at one of the most useful operators from our SJS called the Tap
operator.
It's an operator purely for debugging a pipeline.
It does not affect a stream or the values emitted from an observable.
We can insert the tap operators throughout our pipeline for debugging our operators by checking their
values.
The Tap operator will be given the value emitted by the previous observable or operator.
We can do whatever we want with the value if we attempt to change the value.
The changes are ignored.
It's not an operator meant to impact a value.
Let's use our operator to spy on the values emitted by the interval operator without affecting the underlying
stream.
First, let's import the tap operator from the Oryx J.S. slash operators package.
Next, we will add the tap operator in between the take and scan operators.
Lastly, let's replace the scan operator with the Reduce operator.
In this scenario, we may not want to view the accumulated value over time.
Instead, we may want the total after the observable has been completed.
However, we may want to see the values push from the observable without having to change the operator.
We can apply the tap operator to spy on values.
The Tap operator accepts a function with a supplied value.
Let's log the value from our operator.
In the console, the value emitted by the interval should be logged by using the tap operator.
We've added a checkpoint for viewing the values in our pipeline.
The power of the tap operator can be incredibly helpful for complex operators.
At the moment, we've been writing pipelines with one or two operators.
What if we had 10?
It's possible we might run into problems.
The tap operator can debug the values from various operators as they're being passed through.
One thing to keep in mind is that the tap operator does not affect the underlying stream of data.
If we attempt to change the value, those changes are ignored.
We have limited power with the tap operator.
At the same time, we can manipulate the value without consequence to the overall stream.
There's one last point I want to mention about the tap operator.
Instead of passing in a function, we can pass in an object with the next complete and error functions.
We can secretly insert an observer like object without having to subscribe to the observable.
Let's give it a try.
Wrap the function with curly brackets.
Next, assign the function to a property called Next.
Inside the console, we get the same effect as before.
That's the tap operator, in a nutshell.
In the next set of lectures, we will start getting into more advanced operators, which is why we began
discussing the tap operator.
It's going to help us understand the next set of operators.