Issue
In RxJS, how can I achieve this: I have two subjects. I want an observables that combines them in this way: When both have emitted values, emit the latest of their values. Then, both need to emit at least once again, then emit their latest emitted values, etc.
Both subjects:
1 ---------- 2 ----- 3 -- 4 ---------------- 5 ------ 6 -----------------
------- a ------------------ b ------ c --------------------- d --------
Goal observable:
------- 1a ----------------- 2b ----- 3c ------------------- 4d -------
Solution
This can be done with the zip
function:
import { zip } from 'rxjs'
zip(subject1$, subject2$)
.subscribe(([val1, val2]) => {
console.log(`${val1}${val2}`);
});
The term "zip" comes from zippers. The analogy is that a zipper pairs up successive teeth from each side of the zipper, so too the zip
function pairs up successive values from the observables.
https://rxjs.dev/api/index/function/zip
Answered By - Nicholas Tower
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.