假设有2个可观察量,可观察到的A和可观察到的B. 当A发出一个值时,我想等待1秒,然后发出最新的B值。如果A在等待1秒时发出另一个值,我希望它忘记前一个值并等待另一个1秒(就像switchMap一样) )。 我怎么能实现这样的行为?
答案 0 :(得分:1)
It looks like you could do this easily with the withLatestFrom
operator like the following:
const a$ = ...;
const b$ = ...;
a$
.switchMap(v => Observable.of(v).delay(1000))
.withLatestFrom($b.startWith(null), (a, b) => b)
.subscribe(...);