Node.js
Create a readable stream from an iterable
Use Readable.from to expose an iterable as a Node.js readable stream. It is useful when a downstream API expects a stream but your producer naturally yields records.
Worked example
Node.js example · save as example.mjs and run node example.mjs
import {Readable} from "node:stream";
const input = Readable.from(["one", "two", "three"]);
const values = [];
for await (const value of input) values.push(value);
console.log(values.join(","));How it works
The readable adapts iteration to the stream interface. Async iteration over that stream consumes each emitted value. For larger workflows, an async generator can produce values incrementally instead of building the input array first.
Limits and mistakes
Readable.from normally uses object mode, so downstream transforms need to support the value types you emit. This example collects output only because it has three small values. Retaining all records defeats incremental memory use.
Check the result
Expect one,two,three. Try an async generator that yields after short delays, and check error propagation if the producer throws while being consumed.
Sources
Reference documentation for the behavior used in this guide: