Skip to content

Function: delay()

delay<T>(ms): Operator<T, T>

Defined in: operators/delay.ts:16

Creates a stream operator that delays the emission of each value from the source stream while tracking pending and phantom states.

Each value received from the source is added to context.pendingResults and is only resolved once the delay has elapsed and the value is emitted downstream.

Type Parameters

T

T = any

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

Parameters

ms

number

The time in milliseconds to delay each value.

Returns

Operator<T, T>

An Operator instance for use in a stream's pipe method.

Examples

From delay.spec.ts:4

typescript
const testStream = from([1, 2, 3]);
const delayTime = 100;
const delayedStream = testStream.pipe(delay(delayTime));
const startTime = Date.now();
let emitCount = 0;
delayedStream.subscribe({
  next: () => {
    emitCount++;
    const elapsedTime = Date.now() - startTime + 5;
    expect(elapsedTime).toBeGreaterThanOrEqual(emitCount * delayTime);
  },
  complete: () => {
    expect(emitCount).toBe(3);
    done();
  },
});

Released under the MIT License.