1
votes

Let's say there are 2 observables, observable A and observable B. When A emits a value, I want to wait 1 second, and then emit the latest value of B. If A emits another value while waiting 1 second, I want it to forget about the previous value and wait another 1 second (like switchMap does). How could I achieve such behaviour?

1

1 Answers

1
votes

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(...);