TakeWhile
mirror items emitted by an Observable until a specified condition becomes false

The TakeWhile mirrors the source Observable until such time as some condition you specify becomes false, at which point TakeWhile stops mirroring the source Observable and terminates its own Observable.
See Also
- Skip
- SkipLast
- SkipUntil
- SkipWhile
- Take
- TakeLast
- TakeUntil
- Introduction to Rx: SkipWhile and TakeWhile
Language-Specific Information
RxGroovy takeWhile

The takeWhile
operator returns an Observable that mirrors the behavior of the source Observable until such time as a function, applied to an item emitted by that Observable, returns false
, whereupon the new Observable terminates with an onCompleted
notification.
numbers = Observable.from( [1, 2, 3, 4, 5, 6, 7, 8, 9] ); numbers.takeWhile({ ((it < 6) || (0 == (it % 2))) }).subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
1 2 3 4 5 6 Sequence complete
takeWhile
does not by default operate on any particular Scheduler.
- Javadoc:
takeWhile(Func1)
RxJava 1․x takeWhile

The takeWhile
operator returns an Observable that mirrors the behavior of the source Observable until such time as a function, applied to an item emitted by that Observable, returns false
, whereupon the new Observable terminates with an onCompleted
notification.
takeWhile
does not by default operate on any particular Scheduler.
- Javadoc:
takeWhile(Func1)
RxJS takeWhile

RxJS implements the takeWhile
operator. You pass it a function that governs the takeping process. takeWhile
calls that function for each item emitted by the source Observable until such time as the function returns false
, whereupon takeWhile
stops mirroring the source Observable (starting with that item) and issues an onCompleted
notification. The function takes three parameters:
- the emitted item
- the zero-based index of that item in the sequence of emissions
- the source Observable
You may optionally pass a second parameter to takeWhile
. If so, that item will also be available to your predicate function as “this
”.
Sample Code
var source = Rx.Observable.range(1, 5) .takeWhile(function (x) { return x < 3; }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 0 Next: 1 Next: 2 Completed
takeWhile
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
RxPHP takeWhile takeWhileWithIndex
RxPHP implements this operator as takeWhile
.
Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a a callback to test each source element for a condition. The callback predicate is called with the value of the element.
Sample Code
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/take/takeWhile.php $source = \Rx\Observable::range(1, 5) ->takeWhile(function ($x) { return $x < 3; }); $subscription = $source->subscribe($stdoutObserver);
Next value: 1 Next value: 2 Complete!
RxPHP also has an operator takeWhileWithIndex
.
Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a a callback to test each source element for a condition. The callback predicate is called with the index and the value of the element.
Sample Code
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/take/takeWhileWithIndex.php $source = \Rx\Observable::range(1, 5) ->takeWhileWithIndex(function ($i) { return $i < 3; }); $subscription = $source->subscribe($stdoutObserver);
Next value: 1 Next value: 2 Next value: 3 Complete!
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/takewhile.html