Skip to content

Function: debounce()

debounce<T>(duration): Operator<T, T>

Defined in: operators/debounce.ts:16

Creates a stream operator that emits the most recent value from the source stream only after a specified duration has passed without another new value.

This version tracks pending results in the PipeContext and marks superseded values as phantoms.

Type Parameters

T

T = any

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

Parameters

duration

number

The debounce duration in milliseconds.

Returns

Operator<T, T>

An Operator instance for use in a stream pipeline.

Examples

From debounce.spec.ts:4

typescript
const values = [1, 2, 3, 4, 5];
const debouncedStream = from(values).pipe(debounce(100));
const emittedValues: number[] = [];
debouncedStream.subscribe({
  next: (value: number) => emittedValues.push(value),
  complete: () => {
    // Only the last value should be emitted due to debounce
    expect(emittedValues).toEqual([5]);
    done();
  },
});

Released under the MIT License.