Is-there an opposite of the `race` operator in RxJS?

后端 未结 5 541
轮回少年
轮回少年 2021-01-14 11:48

I have two observables and I want listen to the one that emits its first value last, is there an operator for this ? Something like that :

let obs1 = Rx.Obse         


        
5条回答
  •  旧时难觅i
    2021-01-14 12:03

    How about this?

    let obs1 = Rx.Observable.timer(500,500);
    let obs2 = Rx.Observable.timer(1000,1000);
    
    let sloth = Rx.Observable.race(
        obs1.take(1).concat(obs2),
        obs2.take(1).concat(obs1)
    ).skip(1);
    

    And as a function with multiple Observables support:

    let sloth = (...observables) => 
        observables.length === 1 ?
            observables[0] :
        observables.length === 2 ?
            Rx.Observable.race(
                observables[0].take(1).concat(observables[1]),
                observables[1].take(1).concat(observables[0])
            ).skip(1) :
        observables.reduce((prev, current) => sloth(prev, current))[0];
    

提交回复
热议问题