rxjs里merge operators的用法

334 阅读1分钟

stream是lazy的,no subscription, no calculation occurs.

作用:Flattens multiple Observables together by blending their values into one Observable.

例子:

 const clicks = fromEvent(document, 'click');
    const timer = interval(1000);
    const clicksOrTimer = merge(clicks, timer);
    clicksOrTimer.subscribe(x => console.log('jerry: ' + x));

测试结果:每隔1秒钟Observable emit一个值递增的整数。然后如果我点击UI,会显示一个[object MouseEvent]的事件:

const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(1000).pipe(take(10));
const timer3 = interval(1000).pipe(take(10));
const concurrent = 3; // the argument
const merged = merge(timer1, timer2, timer3, concurrent);
merged.subscribe(x => console.log('diablo: '
 + x));

每秒同时emit 三个值:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":