Skip to main content
Home

Built and signed on GitHub Actions

Well-tested utility functions dealing with async iterables

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
88%
Published
2 years ago (0.6.0)
function tee
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 2,
): [AsyncIterableIterator<T>, AsyncIterableIterator<T>]

Type Parameters

Parameters

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

Return Type

[AsyncIterableIterator<T>, AsyncIterableIterator<T>]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 3,
): [
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Type Parameters

Parameters

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

Return Type

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 4,
): [
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Type Parameters

Parameters

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

Return Type

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 5,
): [
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Type Parameters

Parameters

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

Return Type

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 6,
): [
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Type Parameters

Parameters

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

Return Type

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 7,
): [
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Type Parameters

Parameters

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

Return Type

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: 8,
): [
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]

Type Parameters

Parameters

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

Return Type

[
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
AsyncIterableIterator<T>,
]
tee<T>(
source: Iterable<T> | AsyncIterable<T>,
number: number,
): 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

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.

Return Type

AsyncIterableIterator<T>[]

An array of duplicated async iterables.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@hongminhee/aitertools

Import symbol

import { tee } from "@hongminhee/aitertools/tee";
or

Import directly with a jsr specifier

import { tee } from "jsr:@hongminhee/aitertools/tee";

Add Package

pnpm i jsr:@hongminhee/aitertools
or (using pnpm 10.8 or older)
pnpm dlx jsr add @hongminhee/aitertools

Import symbol

import { tee } from "@hongminhee/aitertools/tee";

Add Package

yarn add jsr:@hongminhee/aitertools
or (using Yarn 4.8 or older)
yarn dlx jsr add @hongminhee/aitertools

Import symbol

import { tee } from "@hongminhee/aitertools/tee";

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

import { tee } from "@hongminhee/aitertools/tee";

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

import { tee } from "@hongminhee/aitertools/tee";

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

import { tee } from "@hongminhee/aitertools/tee";