1
votes

I have 2 Observables, a and b. I want to combine them like so:

a: ---x---------x-------x------|
b: ------x-x-x----x-x-x---x-x--|

o: ------x--------x-------x----|

That is, I want the result Observable to emit on the first emission of b that comes directly after an emission of a.

I'm using reactivex/rxjs, but just a conceptual hint is more than enough. Thank you in advance!

1

1 Answers

1
votes

We can flatMap over the a$ Observable and return a new Observable that emits the first item that we get from b$.

CAVEAT: b$ needs to be a hot Observable. Please read up on hot/cold Observables if this term is unfamiliar: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/creating.md#cold-vs-hot-observables

const firstBAfterA$ = a$.flatMap(x => {
  return b$.first();
});