Rxjs类似于zip的运算符,仅在可观察到的特定顺序触发时返回

时间:2019-03-07 08:51:39

标签: rxjs rxjs6 rxjs-pipeable-operators

假设我有两个Observable A和B,并且我想将它们结合起来以产生这种行为:仅当A已被触发时A才会触发订阅组合。它与zip不同,因为如果A已经被触发然后B被触发,我不希望有任何回报。换句话说:忽略A直到B发射,然后再返回下一个A,然后忽略其他任何A直到B发射...等等

1 个答案:

答案 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));