takeUntil
signature: takeUntil(notifier: Observable): Observable
takeUntil(notifier: Observable): ObservableEmit values until provided observable emits.
๐ก If you only need a specific number of values, try take!
Examples
Example 1: Take values until timer emits
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { interval, timer } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
//emit value every 1s
const source = interval(1000);
//after 5 seconds, emit value
const timer$ = timer(5000);
//when timer emits after 5s, complete source
const example = source.pipe(takeUntil(timer$));
//output: 0,1,2,3
const subscribe = example.subscribe(val => console.log(val));Example 2: Take the first 5 even numbers
( StackBlitz | jsBin | jsFiddle )
Example 3: Take mouse events on mouse down until mouse up
( StackBlitz )
Related Recipes
Additional Resources
takeUntil ๐ฐ - Official docs
takeUntil - In Depth Dev Reference
Avoiding takeUntil leaks - Angular in Depth
Stopping a stream with takeUntil ๐ฅ ๐ต - John Linquist
Build your own takeUntil operator ๐ฅ - Kwinten Pisman
๐ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeUntil.ts
Last updated