- [Instructor] In the last video, we used this unsubscribe
to show how it would throw an error.
Let's go ahead and comment that out before we continue.
Earlier we mentioned that there are other subjects,
so let's talk about those for a minute.
Remember our second subscriber?
We didn't get a console log and a second subscriber
until the next was called later.
What if want to get the last value
passed through that subject
as soon as we subscribe?
Then we want to use the behavior subject.
The behavior subject is just like the subject,
except it requires a starting value.
So let's go ahead and change our subject
to a behavior subject.
If you're using VS Code,
and you hold down the option,
you can actually select several of those
and type them all at once.
Okay, but immediately, we have a red squiggly line.
Why?
Remember I said, the behavior subject
requires a starting value.
So we'll just go ahead and put in a starting value.
The red squiggly goes away.
Now we're not going to change any of the other code here.
I'm going to go ahead and save this.
Let's go back to the console log and look.
Okay, now this looks a bit different.
So we got the first subscribe,
with the initial value,
then the number one,
then the number two.
The second subscribe didn't get the initial value.
It started with the number two.
Why?
If we look at the code again,
any new subscribe will get the latest, current value.
The last value passed in
before the second subscribe was two.
So that's the first value we see for the second subscribe.
So a behavior subject is just like a subject
except it requires a starting value
and holds the most recent value for any new subscribers.