skip to content

iif

 

Using a predicate, decide which Observable to provide at the time of subscription

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const { rxObserver } = require('api/v0.3');
const { iif, of } = require('rxjs');

// decides later which observable to return
const source$ = iif(
  ()=> Date.now() < 3 // predicate
  , of('🐦')          // true
  , of('🦉')          // false
);

// subsctibe at T0
source$.subscribe(rxObserver('Early bird'));

// delayed subscription
setTimeout(()=>{
  source$.subscribe(rxObserver('Night owl'));
}, 5);

0msEarly birdstartcomplete🐦🐦 Night owlstartcomplete🦉🦉

To switch between many Observables see defer function