Search code examples
typescriptrxjsngrxrxjs-observablesrxjs-pipeable-operators

Undefind type not filter in skipWhile?


I had subscribed for async pipe and one pipe retrun - > bservable< AModel | undefined | null>. I compine this four pipe in combineLatest and subcribe.but I need below code excute only -> AModel came. So ,I used skipWhile(([, , , selectedA]) => !selectedA) . but AModel -> undefind it not stop and go to exute below code. need some expert help to resolve it.

------------------code -----------------------------------

   combineLatest(observables)
     .pipe(skipWhile(([, , , selectedA]) => !selectedA),
       shareReplayUntil(this.destroySub))
     .subscribe(([a, b, c, selectedA]) => { }```

Solution

  • skipWhile works only once, this means that when the selectedA value becomes other than null or undefined skipWhile will pass values through afterwards, even if new value of selectedA is undefined.

    In your example, I guess that selectedA value arrives and then it becomes undefined again, but on the second time when the undefined arrives, the skipWhile will not do anything and event will be emitted.

    If you want to avoid selectedA being undefined or null at all times, use filter operator like so:

    combineLatest(observables).pipe(
        filter(([, , , selectedA]) => selectedA !== null && selectedA !== undefined),
        shareReplayUntil(this.destroySub)
      ).subscribe(([a, b, c, selectedA]) => {});