Aaron Shen Aaron Shen - 3 months ago 7
Javascript Question

How to create a stream that emit a value only when another stream emit a value?

I'm learning rxjs.
Assume I have an array of value:

[12, 23, 34, 56, 78, 99]


I want to create a stream
A$
of the above values, but it don't emit values one after another immediately, instead it should wait to emit value only when another stream
B$
emits a value.

Answer

You have several options :

  • use sample, as mentioned in the comments to your question. This means that you will not have access to the value emitted by B$.
  • use combineLatest/zip if you want to reuse the value carried by your stream B$, and some additional semantics. For instance, A$.combineLatest(B$, function (a,b){...})
  • withLatestFrom, but like this : B$.withLatestFrom(A$, function (b,a){...}

All these options have slightly different semantics, so choose them wisely.

Comments