rxjs / 7.5.5 / api / index / function / takewhile.html /

takeWhile

function stable operator

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: boolean = false): MonoTypeOperatorFunction<T>

Parameters

predicate (value: T, index: number) => boolean

A function that evaluates a value emitted by the source Observable and returns a boolean. Also takes the (zero-based) index as the second argument.

inclusive boolean

Optional. Default is false.

When set to true the value that caused predicate to return false will also be emitted.

Returns

MonoTypeOperatorFunction<T>: A function that returns an Observable that emits 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 marble diagram

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, takeWhile } from 'rxjs';

const clicks = fromEvent<PointerEvent>(document, 'click');
const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
result.subscribe(x => console.log(x));

Overloads

takeWhile(predicate: BooleanConstructor, inclusive: true): MonoTypeOperatorFunction<T>

Parameters

predicate BooleanConstructor
inclusive true

Returns

MonoTypeOperatorFunction<T>

takeWhile(predicate: BooleanConstructor, inclusive: false): OperatorFunction<T, TruthyTypesOf<T>>

Parameters

predicate BooleanConstructor
inclusive false

Returns

OperatorFunction<T, TruthyTypesOf<T>>

takeWhile(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>

Parameters

predicate BooleanConstructor

Returns

OperatorFunction<T, TruthyTypesOf<T>>

takeWhile(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>

Parameters

predicate (value: T, index: number) => value is S

Returns

OperatorFunction<T, S>

takeWhile(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction<T, S>

Parameters

predicate (value: T, index: number) => value is S
inclusive false

Returns

OperatorFunction<T, S>

takeWhile(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction<T>

Parameters

predicate (value: T, index: number) => boolean
inclusive boolean

Optional. Default is undefined.

Returns

MonoTypeOperatorFunction<T>

See Also

© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/takeWhile