This generator will repeat another generator up until some condition. This is the version that can handle async generators.
For example, @ixfx/numbers.count will count from 0..number and then finish:
import { count } from '@ixfx/numbers'for (const v of count(5)) { // v: 0, 1, 2, 3, 4} Copy
import { count } from '@ixfx/numbers'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} Copy
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 Copy
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 Copy
const ac = new AbortController();repeat(generator, { signal: ac.signal }); // Pass in signal...ac.abort(); // Trigger signal at some point
This generator will repeat another generator up until some condition. This is the version that can handle async generators.
For example, @ixfx/numbers.count will count from 0..number and then finish:
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"
Limiting the number of repeats can be done by passing in extra parameters