iif
Decides at subscription time which Observable will actually be subscribed.
Parameters
condition |
Condition which Observable should be chosen. |
trueResult |
Optional. Default is Type: |
falseResult |
Optional. Default is Type: |
Returns
Observable<T | F>
: Either first or second Observable, depending on condition.
Description
If
statement for Observables.
iif
accepts a condition function and two Observables. When
an Observable returned by the operator is subscribed, condition function will be called.
Based on what boolean it returns at that moment, consumer will subscribe either to
the first Observable (if condition was true) or to the second (if condition was false). Condition
function may also not return anything - in that case condition will be evaluated as false and
second Observable will be subscribed.
Note that Observables for both cases (true and false) are optional. If condition points to an Observable that was left undefined, resulting stream will simply complete immediately. That allows you to, rather then controlling which Observable will be subscribed, decide at runtime if consumer should have access to given Observable or not.
If you have more complex logic that requires decision between more than two Observables, defer
will probably be a better choice. Actually iif
can be easily implemented with defer
and exists only for convenience and readability reasons.