takeUntil
signature: takeUntil(notifier: Observable): Observable
Example 1: Take values until timer emits
Example 2: Take the first 5 even numbers
import { interval } from 'rxjs/observable/interval';
import { takeUntil, filter, scan, map, withLatestFrom } 'rxjs/operators';
//emit value every 1s
const source = interval(1000);
const isEven = val => val % 2 === 0;
//only allow values that are even
//keep a running total of the number of even numbers out
const evenNumberCount = evenSource.pipe(scan((acc, _) => acc + 1, 0));
//do not emit until 5 even numbers have been emitted
const fiveEvenNumbers = evenNumberCount.pipe(filter(val => val > 5));
const example = evenSource.pipe(
//also give me the current even number count for display
withLatestFrom(evenNumberCount),
takeUntil(fiveEvenNumbers)
)
/*
Even number (1) : 0,
Even number (2) : 2
Even number (3) : 4
Even number (4) : 6
Even number (5) : 8
*/
Additional Resources