Combine PassthroughSubject and CurrentValueSubject

One difference between them is CurrentValueSubject has an initial value and emits the latest value to new subscribers. I will show with an example. First, the PassthroughSubject:

As you see, subscription2 does not get any value since PassthroughSubject does not emit latest value. Now, the CurrentValueSubject:

See that, we have to initialize CurrentValueSubject with a value and subscription1 receives this initial value which was the latest by the time subscription1 was initialized. Also, subscription2 receives the latest value when initialized unlike the one in PassthroughSubject example.

Another difference is that you can get the current value of CurrentValueSubject by using its value property:

Also, you can set the value of CurrentValueSubject by using its value property again. (This is equivalent to send function which can be used for both CurrentValueSubject and PassthroughSubject to set the value)

--

--