Single, race both get only one emit value from the stream.
Single(fn):
const source = Rx.Observable.from([1,2,3,4,5]); var single = source.single( x => x === 3); /* (12345)| (source) single( x => x === 3) 3| (single) */ var sub = single.subscribe( x => console.log(x)); // 3
race(...observable): Observable
const winner = Rx.Observable.race( //emit every 1.5s Rx.Observable.interval(1500), //emit every 1s Rx.Observable.interval(1000).mapTo(‘1s won!‘), //emit every 2s Rx.Observable.interval(2000), //emit every 2.5s Rx.Observable.interval(2500) ).take(1); /** ------0 (1500) ----0 (1000).mapTo(‘1s won!‘) --------0 (2000) ----------0 (2500) race ----(1s won!)| (take(1)) */ const sub2 = winner.subscribe( x => console.log(x)); // 1s won!
时间: 2024-10-13 20:07:07