Skip to main content
Module

x/aitertools/mod.ts>tee

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

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 2

Returns

[AsyncIterableIterator<T>, AsyncIterableIterator<T>]

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 3

Returns

[AsyncIterableIterator<T>, AsyncIterableIterator<T>, AsyncIterableIterator<T>]

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 4

Returns

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 5

Returns

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 6

Returns

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 7

Returns

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Parameters

source: Iterable<T> | AsyncIterable<T>
number: 8

Returns

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Effectively duplicates an async iterable into multiple async iterables. It guarantees that the async iterable source will be iterated in the same order in all the duplicated async iterables, and the source will be only iterated once.

import { tee } from "./tee.ts";

async function* source() {
  console.log("Yielding 1...");
  yield 1;
  console.log("Yielding 2...");
  yield 2;
  console.log("Yielding 3...");
  yield 3;
}

const [a, b, c] = tee(source(), 3);
for await (const value of a) console.log('a:', value);
for await (const value of b) console.log('b:', value);
for await (const value of c) console.log('c:', value);

The above example will print the following:

Yielding 1...
a: 1
Yielding 2...
a: 2
Yielding 3...
a: 3
b: 1
b: 2
b: 3
c: 1
c: 2
c: 3

Note that console.log() calls in the source function are executed only once, and not three times. Also these numbers are always in the same order.

Type Parameters

T

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

Parameters

source: Iterable<T> | AsyncIterable<T>

The async iterable to duplicate.

number: number

The number to duplicate the async iterable.

Returns

AsyncIterableIterator<T>[]

An array of duplicated async iterables.