Function repeat

  • Generates values from produce with a time delay. produce can be a simple function that returns a value, an async function, or a generator. If produce returns undefined, generator exits.

    Type Parameters

    Parameters

    Returns AsyncGenerator<T>

    Returns value of produce function

    Produce a random number every 500ms

    const randomGenerator = repeat(() => Math.random(), 500);
    for await (const r of randomGenerator) {
    // Random value every 1 second
    // Warning: does not end by itself, a `break` statement is needed
    }

    Return values from a generator every 500ms

    import { repeat } from 'https://unpkg.com/ixfx/dist/flow.js'
    import { count } from 'https://unpkg.com/ixfx/dist/numbers.js'
    for await (const v of repeat(count(10), { fixed: 1000 })) {
    // Do something with `v`
    }

    Options allow either fixed interval (wait this long between iterations), or a minimum interval (wait at least this long). The latter is useful if produce takes some time - it will only wait the remaining time or not at all.

    If the AbortSignal is triggered, an exception will be thrown, stopping iteration.

    • continuously: loop that runs at a constant speed. Able to be started and stopped
    • repeat: run a function a certain number of times, collecting results