Skip to main content
Module

x/aitertools/mod.ts>toArray

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

Creates an array from an async iterable.

import { toArray } from "./collections.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; }
const array = await toArray(gen());

The array variable will be an array like ["foo", "bar", "baz"].

Note that its first parameter is assumed to be finite; otherwise, it will never return. The following example will never return:

import { toArray } from "./collections.ts";
import { count } from "./infinite.ts";

await toArray(count(0));

Type Parameters

T

The type of the elements in the source and the returned array.

Parameters

source: AsyncIterable<T>

An async iterable to create an array from. It must be finite.

Returns

Promise<T[]>

An array that contains the elements from the source iterable.