Skip to content

Function: takeUntil()

takeUntil<T, R>(notifier): Operator<T, T>

Defined in: operators/takeUntil.ts:22

Creates a stream operator that emits all values from the source stream until a value is emitted by a notifier stream.

This operator controls the lifespan of a stream based on an external signal. It consumes and re-emits values from the source until the notifier stream emits its first value. As soon as that happens, the operator completes the output stream and unsubscribes from both the source and the notifier.

This is useful for automatically stopping an operation when a certain condition is met, such as waiting for a user to close a dialog or for an animation to complete.

Type Parameters

T

T = any

The type of the values in the source and output streams.

R

R = T

Parameters

notifier

The stream that, upon its first emission, signals that the operator should complete.

Stream<R> | Promise<R>

Returns

Operator<T, T>

An Operator instance that can be used in a stream's pipe method.

Examples

From takeUntil.spec.ts:4

typescript
const testStream = from([1, 2, 3]);
const notifier = timer(2000, 1000).pipe(take(1));
const takenUntilStream = testStream.pipe(takeUntil(notifier));
let results: any[] = [];
takenUntilStream.subscribe({
  next: (value) => results.push(value),
  complete: () => {
    expect(results).toEqual([1, 2, 3]); // Should emit all values before notifier emits
    done();
  },
});

Released under the MIT License.