Angular RxJS: conditional operator (if else) Angular RxJS: conditional operator (if else) angular angular

Angular RxJS: conditional operator (if else)


You can decide which observable return inside the switchMap operator, like this:

    this.deactivate$        .pipe(            switchMap((canDeactivate) => {               if (canDeactivate) {                   return Observable.of(canDeactivate);               }               else {                   return Observable.of(window.confirm("message"));               }            })        );

Bonus (super shorter version):

this.deactivate$.pipe(        switchMap((canDeactivate) => Observable.of(canDeactivate || window.confirm("message"))    );


From documentation of rxjs

iif Decides at subscription time which Observable will actually be subscribed.

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.

example

let accessGranted;const observableIfYouHaveAccess = iif(  () => accessGranted,  of('It seems you have an access...'),  of('Opps'));

So if accessGranted is true it will execute the first of else second of


window.confirm is blocking so you can just use a map() operator. Calling switchMap to of(window.confirm(..)) will just stop the execution of JavaScript anyway.

this.deactivate$    .pipe(        map(canDeactivate => canDeactivate ? canDeactivate : window.confirm("message"))    );