The iif
operator from the rxjs
library allows you to conditionally subscribe to one observable sequence or another based on a condition.
Here's an example of how to use it:
index.tsx463 chars18 lines
In this example, we're using iif
to generate a random number between 0 and 9, and then checking if it's even. If it is, we subscribe to an observable that emits the string 'Even number'
, otherwise we subscribe to an observable that emits the string 'Odd number'
.
We then use mergeMap
and tap
operators to log the emitted values to the console.
Note that iif
takes three arguments: the condition (a function that returns a boolean value), the observable to subscribe to if the condition is true, and the observable to subscribe to if the condition is false.
gistlibby LogSnag