我有以下
const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const merged = merge(timer1, timer2);
merged.subscribe(x => console.log(x));
现在,这将仅提供可观察到的最新发射的值。
我想,当一个发出时,我得到了新的价值,而另一个则得到了最新的价值。
我尝试使用,zip
但不会使用最新的。在下面的屏幕截图中查看我希望拥有的缺失值。我怎样才能做到这一点?
联接将起作用,但如果相同的可观察对象在另一个变量发出一个值之前发出了两次,则联接不会发出。
您正在寻找combineLatest
。
combineLatest
每当观察到的两个源中的任何一个发出时,都将发出。注意:直到每个可观察到的物体至少发射了一次,它才会第一次发射。
因此,以您的示例为例,它看起来可能像这样(StackBlitz):
const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const merged = combineLatest([timer1, timer2]);
merged.subscribe(([one, two]) => console.log(`${one}-${two}`));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句