If I use, 2 gets printed to the console every 3 seconds:
Rx.Observable.interval(3000)
.mergeMap(() => Rx.Observable.of(3))
.concatMap(()=>Rx.Observable.of(2).first(x=>x == 2))
.subscribe((x)=>console.log(x))
If I use, 2 gets printed to the console only one time:
Rx.Observable.interval(3000)
.mergeMap(() => Rx.Observable.of(3))
.concatMap(()=>Rx.Observable.of(2))
.first(x=>x == 2)
.subscribe((x)=>console.log(x))
The difference is where I chained the first operator, but I don't understand the flow of the chain. I thought in both cases, it should only print 2 once since I have an observable of 2.
You are confused with the
first()
operator. The definition offirst()
is:Note the word observable sequence, aka stream. The difference of the two scenarios are as follow:
Scenario 1:
Your
first()
is applied to an observable that returns a value 2. Nothing is done to theObservable.interval()
stream, which keeps on emitting an event every 3 seconds. That is why your console never stops logging.Scenario 2:
The
first()
is applied to anObservable.interval()
that continuously emits an event, every 3 seconds. Hence, yourfirst()
operator basically chops the sequence (or stop it) as soon as it finds the fulfilling condition, which isx===2
. When the condition is met, the stream ends, it then returns the FIRST element of that sequence. Hence your console only logs one time.