Clément Flodrops
Clément Flodrops

Reputation: 1104

RxJS: combine observables, emit when source emits and retrieve last value of others

I'm looking for an operator / a way to build an observable with rxjs but I don't understand how to do this.

I have two observables: A and B.

I want to combine them this way : whenever A emits (and just A), I can subscribe to the latest value of A and B.

A: ----1----- 2-------------3--4----5--------6-------|-->
B: --a----------b----a----------------b--------------|-->
// something
C: ----1a-----2a------------3a-4a---5a-------6b------|-->

Thanks for your help !

Upvotes: 2

Views: 442

Answers (2)

Clément Flodrops
Clément Flodrops

Reputation: 1104

According to rxmarbles : http://rxmarbles.com/#withLatestFrom and the official doc I was looking for withLatestFrom.

I've tried to use it but it seems that it won't work. I must use it the wrong way :-) maybe it's because of a stupid mistake !

Upvotes: 0

Mark van Straten
Mark van Straten

Reputation: 9425

You can use withLatestFrom to do exactly what you need

const stream1 = Rx.Observable.interval(100)
  .map(i => String.fromCharCode(97 + (i % 26)));
const stream2 = Rx.Observable.interval(250);
stream1.withLatestFrom(stream2, (x,y) => `${x}:${y}`)
  .take(20)
  .subscribe(console.log);

Upvotes: 1

Related Questions