Home > Net >  Observable wait until another observable value
Observable wait until another observable value

Time:07-15

I have one observable A and another observable B (that is a BehaviorSubject). I need that B works like a semaphore, when I get a value changes in A, if B is true, A executes some logic, otherwise A need to wait B become true. I tried different approaches, but without success..

Something like when in C is used sem_wait for waiting 0 value

A
 .pipe(
    mergeMap(aval => B.filter(f => f))
   // Here i need to wait B to become true
 )
 .subscribe(v => dosomething)

Thanks to all!

CodePudding user response:

Check this. The code below will invoke every A event when B event become true (if you want only one A event at a time just change the mergeMap operator to the switchMap and if you want proper order to be preserved change it to the concatMap and move dosomething to the tap operator below the take(1) line)

A.pipe(
    mergeMap(aval => B.pipe(
        filter(f => f), 
        map(()=> aval),
        take(1) // you need to unsubscribe the inner observable
        )
    )
) 
.subscribe(v => dosomething)
  • Related