Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/hex/src/generator/deps.ts>streams.readableStreamFromIterable

An ecosystem delivering practices, philosophy and portability.
Go to Latest
function streams.readableStreamFromIterable
import { streams } from "https://deno.land/x/hex@0.5.15/src/generator/deps.ts";
const { readableStreamFromIterable } = streams;

Create a ReadableStream from any kind of iterable.

     import { readableStreamFromIterable } from "https://deno.land/std@0.224.0/streams/conversion.ts";

     const r1 = readableStreamFromIterable(["foo, bar, baz"]);
     const r2 = readableStreamFromIterable(async function* () {
       await new Promise(((r) => setTimeout(r, 1000)));
       yield "foo";
       await new Promise(((r) => setTimeout(r, 1000)));
       yield "bar";
       await new Promise(((r) => setTimeout(r, 1000)));
       yield "baz";
     }());

If the produced iterator (iterable[Symbol.asyncIterator]() or iterable[Symbol.iterator]()) is a generator, or more specifically is found to have a .throw() method on it, that will be called upon readableStream.cancel(). This is the case for the second input type above:

import { readableStreamFromIterable } from "https://deno.land/std@0.224.0/streams/conversion.ts";

const r3 = readableStreamFromIterable(async function* () {
  try {
    yield "foo";
  } catch (error) {
    console.log(error); // Error: Cancelled by consumer.
  }
}());
const reader = r3.getReader();
console.log(await reader.read()); // { value: "foo", done: false }
await reader.cancel(new Error("Cancelled by consumer."));

Parameters

iterable: Iterable<T> | AsyncIterable<T>