What happens when filter is passed in pipe?
I am trying to understand why the below subscription is not working:
isLoggedIn1$:Observable<boolean> = of(false) // on subscribing it I get value as false
isLoggedIn2$:Observable<boolean> = of(false).pipe(filter(value => Boolean(value))) // this one is not giving any value even it looks like subscribe is not getting called for this
Is there any documentation for this particular behavior?
As I understand it is being treated equivalent to EMPTY
. but then why I am not getting type error for second one.
Solution 1:
The filter
operator filters the value based on given predicate. If that condition specified by filter
is fulfilled, then the value will pass down to subscriber else we don't see anything on screen.
For the line
isLoggedIn2$:Observable<boolean> = of(false).pipe(filter(value => Boolean(value)))
The predicate here only allows those value to pass further down the pipe that are true
value => Boolean(value)
If you want to print false
you have to modify filter condition as
value => Boolean(value) === false
More on filter operator - https://www.learnrxjs.io/learn-rxjs/operators/filtering/filter