[RxJS] Filtering operators: skipWhile and skipUntil

After takeUntil() and takeWhile() function, let‘s have a look on skipWhile() and skilUntil() functions.

SkipWhile(predicate: function): Skip the value if meet the predicate function.

var foo = Rx.Observable.interval(1000);
/*
--0--1--2--3--4--5--6--7--...
  skipWhile(x => x < 3).take(3)
-----------3--4--5|
*/

var bar = foo.skipWhile((x)=>{
             return x<3;
         }).take(3);

bar.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);

  /*
"next 3"
"next 4"
"next 5"
"done"
  */

skipUntil(Observable): after 3 seconds, click the start button, to start the foo observalbe.

var foo = Rx.Observable.interval(1000);
var start$ = Rx.Observable.fromEvent(document.querySelector(‘#start‘), ‘click‘);
/*
--0--1--2--3--4--5--6--7--...
  skipUntil(start$)
-----------3--4--5|
*/

var bar = foo.skipUntil(start$);

bar.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);

  /*
"next 3"
"next 4"
"next 5"
"done"
  */
时间: 2024-11-05 11:29:29

[RxJS] Filtering operators: skipWhile and skipUntil的相关文章

[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] 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过滤数据流操作符学习 (Filtering Operators)

1. 过滤类操作符的模式 很多时候,上游Observable对象吐出的数据,并不是下游关心的,这个时候我们需要把不需要的数据,过滤掉.在RxJS中,提供了这类过滤操作符来实现这种功能. 过滤类操作符最基本的功能就是对一个给定的数据流中的每个数据判断是否满足某个条件,如果满足就传递给下游,如果不满足就抛弃. 判断一个数据是否有资格进入下游,是根据 "判定函数"的返回值, true代表可以进入下游,否则就会被淘汰. 有的过滤类操作符还可以接受一个函数参数"结果选择器",

[RxJS] Creation operators: empty, never, throw

This lesson introduces operators empty(), never(), and throw(), which despite being plain and void of functionality, are very useful when combining with other Observables. empty: var foo = Rx.Observable.empty(); // the same as var foo = Rx.Observable

[RxJS] Transformation operators: delay and delayWhen

This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | date) var foo = Rx.Observable.interval(500).take(5); /* --0--1--2--3--4| delay(1000) -----0--1--2--3--4| */ // delay(1000) var result = foo.delay(1000);

[RxJS] Creation operators: interval and timer

It is quite common to need an Observable that ticks periodically, for instance every second or every 100 miliseconds. We will learn about operators interval() and timer(), both of which are similar to setInterval() in JavaScript. Interval(period): Yo

[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

[RxJS] Creation operators: from, fromArray, fromPromise

The of() operator essentially converted a list of arguments to an Observable. Since arrays are often how we structure list of things in JavaScript, we should have a way of transforming arrays into Observables. This lesson teaches how you can convert

[RxJS] Combination operators: concat, startWith

Some Observables may complete, and we may want to append another Observable to the one which just completed. This lesson teaches you how to use the concat() operator for either appending or prepending, and how the shortcut operator startWith() is an