takeWhile
Emits values emitted by the source Observable so long as each value satisfies
the given predicate
, and then completes as soon as this predicate
is not
satisfied.
takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive: false = false): MonoTypeOperatorFunction<T>
Parameters
Returns
MonoTypeOperatorFunction<T>
: An Observable that emits the values from the source
Observable so long as each value satisfies the condition defined by the
predicate
, then completes.
Description
Takes values from the source only while they pass the condition given. When the first value does not satisfy, it completes.
takeWhile
subscribes and begins mirroring the source Observable. Each value
emitted on the source is given to the predicate
function which returns a
boolean, representing a condition to be satisfied by the source values. The
output Observable emits the source values until such time as the predicate
returns false, at which point takeWhile
stops mirroring the source
Observable and completes the output Observable.
Example
Emit click events only while the clientX property is greater than 200
import { fromEvent } from 'rxjs';
import { takeWhile } from 'rxjs/operators';
const clicks = fromEvent(document, 'click');
const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
result.subscribe(x => console.log(x));