takeUntil

signature: takeUntil(notifier: Observable): Observable



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

Additional Resources