How can I make an observable based on 2 others, but emit only when the 2 observables emit new values #7112
-
How can I make an observable based on 2 others observables, but emit only when the 2 observables emit new values (regardless of whether it is equal to the previous value emitted), similar to input: import { of, delay, concatMap } from 'rxjs';
const observable1$ = of('a', 'a', 'b').pipe(
concatMap((v) => of(v).pipe(delay(100)))
);
const observable2$ = of(1, 2, 3, 4).pipe(
concatMap((v) => of(v).pipe(delay(100)))
); That is:
Expexted output: ["a", "1"]
["a", "2"]
["b", "3"] With import { combineLatestWith, of, delay, concatMap } from 'rxjs';
const observable1$ = of('a', 'a', 'b').pipe(
concatMap((v) => of(v).pipe(delay(100)))
);
const observable2$ = of('1', '2', '3', '4').pipe(
concatMap((v) => of(v).pipe(delay(100)))
);
observable1$.pipe(combineLatestWith(observable2$)).subscribe(console.log); ["a", "1"]
["a", "1"]
["a", "2"]
["b", "2"]
["b", "3"]
["b", "4"] |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Pipe each one using “distinctUntilChanged” then “combineLatest”? |
Beta Was this translation helpful? Give feedback.
-
Hi @MiniSuperDev ! If I understood the question correctly, then I think that the operator that you are looking for is import { zip, of, delay, concatMap } from 'rxjs';
const observable1$ = of('a', 'a', 'b').pipe(
concatMap((v) => of(v).pipe(delay(100)))
);
const observable2$ = of('1', '2', '3', '4').pipe(
concatMap((v) => of(v).pipe(delay(100)))
);
zip(observabl1, observable2).subscribe(console.log); Just be cautious of backpressure. |
Beta Was this translation helpful? Give feedback.
Hi @MiniSuperDev !
If I understood the question correctly, then I think that the operator that you are looking for is
zip
:Just be cautious of backpressure.