[RxJS] Filtering operator: single, race

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

[RxJS] Filtering operator: single, race的相关文章

[RxJS] Filtering operator: filter

This lesson introduces filter: an operator that allows us to let only certain events pass, while ignoring others. var foo = Rx.Observable.interval(1000); /* --0--1--2--3--4--5--6--7- filter(x => x % 2 === 0) --0-----2-----4-----6---- */ var bar = foo

[RxJS] Filtering operators: distinct and distinctUntilChanged

Operator distinct() and its variants are an important type of Filtering operator. This lessons shows how they work and in what cases are they useful. distinctUntilChanged(): var foo = Rx.Observable.interval(500).take(5) .zip(Rx.Observable.of('a','b',

[RxJS] Connection operator: multicast and connect

We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple observers. However, this technique requires some laborious setting up. In this lesson we will learn about the multicast() operator which helps solve the s

[RxJS] Utility operator: do

We just saw map which is a transformation operator. There are a couple of categories of operators, such as filtering, combination, flattening, etc. One of these categories is the utility operators. The most important utility operator is do, useful fo

[RxJS] Creation operator: of()

RxJS is a lot about the so-called "operators". We will learn most of the important operators, one by one. In this lesson, we will see our first creation operator: of(). var foo = Rx.Observable.of(42, 100, 200); // var bar = Rx.Observable.create(

[RxJS] Transformation operator: map and mapTo

We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't need it because it's too specific: it only does simple multiplication on numbers. In this lesson we will see how the map() operator is useful for any cal

[RxJS] Filtering operators: throttle and throttleTime

Debounce is known to be a rate-limiting operator, but it's not the only one. This lessons introduces you to throttleTime and throttle, which only drop events (without delaying them) to accomplish rate limiting. throttleTime(number): first emits, then

[RxJS] Transformation operator: scan

All of the combination operators take two or more observables as input. These operators may also be alternatively called "vertical combination operators", because of how they work in a marble diagram. Next, we will learn about scan(), which is a

[RxJS] Combination operator: withLatestFrom

Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLatestFrom, another AND-style combination operator, and how it works essentially as map() operator, with some combination properties. var foo = Rx.Observa