简体   繁体   English

Rxjs操作员switchMap

[英]Rxjs operator switchMap

I'm new with Rxjs and write some examples for better understanding. 我是Rxjs的新手,并写了一些示例以更好地理解。 However, the example below confuses me, why the output is 0 and 1? 但是,下面的示例使我感到困惑,为什么输出为0和1? Can someone explain it? 有人可以解释吗?

var sources = Rx.Observable.timer(1000,1000);
var higherOrder = sources.switchMap(val => Rx.Observable.of(val).delay(1000*val));
higherOrder.subscribe(x => console.log(x));

In additon: 另外:

var sources = Rx.Observable.timer(1000,1000);
var higherOrder = sources.switchMap(val => Rx.Observable.of(val));
higherOrder.subscribe(x => console.log(x));

the outputs are : 0 1 2 3 4 5 ... 输出为:0 1 2 3 4 5 ...

switchMap will abandon (unsubscribe) the previous emitted observable once a new one arrives. 新的到达后, switchMap将放弃(取消订阅)先前发出的可观测对象。 So eventually your values are delayed long enough for your timer to emit a new value creating a new Observable for your switchMap to subscribe to abandoning the previous one before retrieving its value. 因此,最终您的值被延迟足够长的时间,以使您的计时器发出新值,从而创建一个新的Observable,以便您的switchMap订阅以放弃获取前一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM