Function delay

  • Delays each value by the specified timeout (including the first one). When the timeout is a negative number, no delay added.

    import {pipe, toAsync, delay} from 'iter-ops';

    const data = [1, 2, 3, 4, 5]; // some synchronous data

    const i = pipe(
    toAsync(data), // make asynchronous
    delay(1000)
    );

    for await(const a of i) {
    console.log(a); //=> 1, 2, 3, 4, 5 (with 1s delay)
    }

    Note that passing in timeout = 0, or returning the same from the callback, still uses the timeout function, just like setTimeout(0), which can be used for throttling. In order to fully deactivate, it needs a negative timeout.

    If you do not want to delay the first value, check out interval.

    Type Parameters

    • T

    Parameters

    • timeout: number

    Returns Operation<T, T>

    Error: 'Operator "delay" requires asynchronous pipeline' when used inside a synchronous pipeline.

  • Delays each value by the specified timeout (as returned from the callback). When the timeout is a negative number, it is not added.

    Note that it doesn't support return of Promise<number> on purpose, to avoid confusion with what operator throttle does.

    Type Parameters

    • T

    Parameters

    • cb: ((value: T, index: number, state: IterationState) => number)
        • (value, index, state): number
        • Parameters

          Returns number

    Returns Operation<T, T>

    Error: 'Operator "delay" requires asynchronous pipeline' when used inside a synchronous pipeline.