假设我有两个Observable A和B,并且我想将它们结合起来以产生这种行为:仅当A已被触发时A才会触发订阅组合。它与zip不同,因为如果A已经被触发然后B被触发,我不希望有任何回报。换句话说:忽略A直到B发射,然后再返回下一个A,然后忽略其他任何A直到B发射...等等
答案 0 :(得分:0)
我相信您需要withLatestFrom
运算符:
import { withLatestFrom, map } from 'rxjs/operators';
import { interval } from 'rxjs';
const obsA = interval(2000);
const obsB = interval(1000);
const resultingObs = obsB.pipe(
withLatestFrom(obsA),
map(([bValue, aValue]) => {
return aValue
})
);
// This should emit values from obsA only when obsB has been fired.
const subscribe = resultingObs .subscribe(val => console.log(val));