来自同一来源的RxJS CombineLatest

时间:2018-08-08 08:12:06

标签: rxjs

const s$ = new Rx.Observable.interval(100).take(4);

const s1$ = s$.map(v => 's1:' + v);
const s2$ = s$.map(v => 's2:' + v);

Rx.Observable.combineLatest(s1$, s2$)
  .subscribe((value) => {
    console.log(value);
  })

我想要这样的输出:

s1: 1, s2: 1
s1: 2, s2: 2
s1: 3, s2: 3

但实际输出:

“ s1:0,s2:0”

“ s1:1,s2:0”

“ s1:1,s2:1”

“ s1:2,s2:1”

“ s1:2,s2:2”

“ s1:3,s2:2”

“ s1:3,s2:3”

我该怎么办?

1 个答案:

答案 0 :(得分:0)

改为使用zip运算符:

Rx.Observable.zip(s1$, s2$)
  .subscribe((value) => {
  console.log(value);
})