import { tee } from "https://deno.land/x/aitertools@0.6.0/src/tee.ts";
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.