RxJS takeWhile but include the last value

后端 未结 6 931
情书的邮戳
情书的邮戳 2020-12-06 04:16

I have a RxJS5 pipeline looks like this

Rx.Observable.from([2, 3, 4, 5, 6])
  .takeWhile((v) => { v !== 4 })

I want to keep the subscrip

6条回答
  •  被撕碎了的回忆
    2020-12-06 05:12

    You can use endWith(value) which (unlike a lot of RxJS code) is very nicely self documenting.

    const example = source.pipe(
                                takeWhile(val => val != 4), 
                                endWith(4));
    

    PS. Also note that takeUntil doesn't take a predicate, so if you were trying to use that operator to solve this problem you can't. It's a whole different method signature.

    Official docs: https://rxjs-dev.firebaseapp.com/api/operators/endWith

    https://stackblitz.com/edit/typescript-pvuawt

提交回复
热议问题