Search code examples
angularrxjsangular2-observablesiif

RXJS conditionally call observable


  const observable$ = iif(() => this.shippingTabModified,
      //Shipping Tab Modified True
      of(this.updateShippingInfo())
        .pipe(),
      //IF Shipping Tab Not Modified    
      of(this.shippingInfoService.getShipmentInfoByCardIdObservable(this.data.CardId))
        .pipe()
    )

    observable$.subscribe();

From above code i want to achieve following scenario

if(foo === true)
{
  updatemethod()
}
// if foo is true and update method is executed we need to wait until update method is finished then go to second method

// If foo is false we can call second method without waiting
secondmethod();

But from rxjs code which i wrote above, it always go to update method it is not considering the value of this.shippingTabModified


Solution

  • You can leverage on the concat function and do something like this

    const observables$ = condition ? [obs$1, obs$2] : [obs$2];
    
    concat(...observables$).subscribe();
    

    Cheers