The take_while operator emits items from the source observable until a criterion is met. The following figure shows the marble diagram of this operator:
Figure 9.34: The take_while operator
Its prototype is the following:
Observable.take_while(self, predicate)
Here, predicate is a function called each time an item is emitted form the source observable. When the predicate function returns True, then the take_while operator stops emitting items of the source observable.
Here is an example of the take_while operator:
numbers = Observable.from_([1, 2, 3, 4])
numbers.take_while(lambda i: i < 2).subscribe(
on_next = lambda i: print("on_next {}".format(i)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)
This example gives the following result: