Skip to content

Function: throttle()

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

Defined in: operators/throttle.ts:16

Creates a throttle operator that emits the first value immediately, then ignores subsequent values for the specified duration. If new values arrive during the cooldown, the last one is emitted after the cooldown expires (trailing emit).

This version tracks pending results and phantoms in PipeContext.

Type Parameters

T

T = any

The type of values emitted by the source and output.

Parameters

duration

number

The throttle duration in milliseconds.

Returns

Operator<T, T>

An Operator instance that applies throttling to the source stream.

Examples

From throttle.spec.ts:4

typescript
const output: number[] = [];
const subject = createSubject<number>();
const iter = eachValueFrom(subject.pipe(throttle<number>(50)));
(async () => {
  for await (const v of iter) {
    output.push(v);
  }
})();
subject.next(1);
subject.next(2);
await new Promise((r) => setTimeout(r, 30));
subject.next(3);
await new Promise((r) => setTimeout(r, 30));
subject.next(4);
subject.complete();
await new Promise((r) => setTimeout(r, 50));
expect(output[0]).toBe(1);
expect(output).toContain(3);
expect(output).toContain(4);
expect(output.length).toBe(3);

Released under the MIT License.