takeUntil

signature: takeUntil(notifier: Observable): Observable

Emit values until provided observable emits.


๐Ÿ’ก If you only need a specific number of values, try take!


Ultimate RxJS

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 )

Additional Resources


๐Ÿ“ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeUntil.ts

Last updated