RxJS Functions (Part 12)

Corey Pyle
1 min readJan 20, 2019

--

iif

iif

https://rxjs.dev/api/index/function/iif

Decides at subscription time which Observable will actually be subscribed.

TL;DR

The function description from the docs is pretty clear for this one. You setup a condition and two Observables as arguments to iif. When you subscribe to the Observable that iif creates, if the condition evaluates to true, then the observer is subscribed to the first Observable. Otherwise its subscribed to the second one.

Use Case

The docs point out that iif is basically a convenience function for defer. They also mention a more interesting use than the main one listed above. If you do not provide one of the Observable arguments to iif, the Observable completes immediately. You can take advantage of this for a quick authentication check:

Thanks for reading. Stay tuned for Part 13, and be sure to checkout my previous articles.

--

--