This generator will repeat another generator up until some condition. This is the version that can handle async generators.

For example, count will count from 0..number and then finish:

for (const v of count(5)) {
// v: 0, 1, 2, 3, 4
}

But what if we want to repeat the count? We have to provide a function to create the generator, rather than using the generator directly, since it's "one time use"

for await (const v of repeat(() => count(5))) {
// v: 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, ...
// warning: never ends
}

Limiting the number of repeats can be done by passing in extra parameters

repeat(generator, { count: 5} ); // Iterate over `generator` five times
const ac = new AbortController();
repeat(generator, { signal: ac.signal }); // Pass in signal
...
ac.abort(); // Trigger signal at some point
  • Type Parameters

    • T

    Parameters

    • genCreator: (() => Iterable<T> | AsyncIterable<T>)
        • (): Iterable<T> | AsyncIterable<T>
        • Returns Iterable<T> | AsyncIterable<T>

    • repeatsOrSignal: number | AbortSignal

    Returns AsyncGenerator<T, any, unknown>