So what I want to achieve here is to get the result of combineLatest([subject1$, subject2$])
only when source$
emits a value.
The problem is that whichever operator I use the subscription gets lunched any time subject1$
or subject2$
emits a value (that's how switchMap
works). Now, I'm having a hard time in finding a suitable operator for this, I guess I'm looking more for some sort of pattern.
const source$ = fromEvent(document, 'click');
const subject1$ = new Subject() // can emit one or more values in unknown time
const subject2$ = new Subject() // can emit one or more values in unknown time
setInterval(() => { subject1$.next(1) }, 1000)
setInterval(() => { subject2$.next('a') }, 2000)
source$
.pipe(
switchMap(() => combineLatest([subject1$, subject2$])), // obviously does not work
)
.subscribe(([subject1, subject2]) => {
console.log(subject1, subject2);
});
Many thanks,
Use withLatestFrom
source$.pipe(
withLatestFrom(combineLatest([subject1$, subject2$]))
)
.subscribe(([_,[subject1, subject2]]) => {
console.log(subject1, subject2);
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.