Skip to main content
Module

x/aitertools/mod.ts>cycle

Well-tested utility functions dealing with async iterables
Go to Latest
function cycle
import { cycle } from "https://deno.land/x/aitertools@0.5.0/mod.ts";

Makes an async iterator that yields elements from the source and saving a copy of each. When the source is exhausted, yields saved copies indefinitely.

Note that it may require significant memory to save the copies depending on the length of the source.

import { cycle } from "./infinite.ts";

async function* gen() { yield 3; yield 6; yield 9; }
const iterable = cycle(gen());
for await (const value of iterable) console.log(value);

The above example will print the following and keep going forever:

3
6
9
3
6
9
(...)

Type Parameters

T

The type of the elements in the source and the returned async iterable.

Parameters

source: Iterable<T> | AsyncIterable<T>

An async iterable to repeat.

Returns

AsyncIterableIterator<T>

An async iterable that repeats the source indefinitely.