squgeim
squgeim

Reputation: 2351

Only activate observable if another observable has emitted

Here's the scenario: there are two observable streams (A and B). I want to only emit from B after A has emitted at least once.

I tried combineLatest, but the problem with that was that the combined stream emitted when either A or B emitted. I only want this stream to emit when B emits, I do not need the value from A. I just need it to have been emitted once.

This is what I am looking for:

---a---a-- A
-b---b---- B
---b-b---- required

Upvotes: 1

Views: 59

Answers (2)

Andrei Gătej
Andrei Gătej

Reputation: 11934

I think this should work:

combineLatest(a$.pipe(first()), b$)
  .pipe(
    map(([a, b]) => b)
  )

combineLatest will emit for the first time when both a$ and b$ would have emitted, then it will only emit when b$ emits, due to a$.pipe(first()).

Upvotes: 2

Adrian Brand
Adrian Brand

Reputation: 21638

Once the combine latest emits then switch back to b.

combineLatest(a$, b$).pipe(
  map(([a, b]) => b),
  switchMap(_ => b$)
);

Upvotes: 0

Related Questions