ReadableStream: from() static method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Note: This feature is available in Web Workers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The ReadableStream.from() static method returns a ReadableStream from a provided iterable or async iterable object.

The method can be used to wrap iterable and async iterable objects as readable streams, including arrays, sets, arrays of promises, async generators, ReadableStreams, Node.js readable streams, and so on.

Syntax

js
ReadableStream.from(anyIterable)

Parameters

anyIterable

An iterable or async iterable object.

Return value

A ReadableStream.

Exceptions

TypeError

Thrown if the passed parameter is not an iterable or async iterable (does not define the [Symbol.iterator]() or [Symbol.asyncIterator]() method). Also thrown if, during iteration, the result of the next step is not an object or is a promise that does not resolve to an object.

Examples

Convert an async iterator to a ReadableStream

This live example demonstrates how you can convert an async iterable to a ReadableStream, and then how this stream might be consumed.

HTML

The HTML is consists of single <pre> element, which is used for logging.

html
<pre id="log"></pre>

JavaScript

The example code creates a log() function to write to the log HTML element.

js
const logElement = document.getElementById("log");
function log(text) {
  logElement.innerText += `${text}\n`;
}

It then checks if the static method is supported, and if not, logs the result.

js
if (!ReadableStream.from) {
  log("ReadableStream.from() is not supported");
}

The async iterable is an anonymous generator function that yields the values of 1, 2 and 3 when it is called three times. This is passed to ReadableStream.from() to create the ReadableStream.

js
// Define an asynchronous iterator
const asyncIterator = (async function* () {
  yield 1;
  yield 2;
  yield 3;
})();

// Create ReadableStream from iterator
const myReadableStream = ReadableStream.from(asyncIterator);

Using readable streams demonstrates several ways to consume a stream. The code below uses a for ...await loop, as this method is the simplest. Each iteration of the loop logs the current chunk from the stream.

js
consumeStream(myReadableStream);

// Iterate a ReadableStream asynchronously
async function consumeStream(readableStream) {
  for await (const chunk of readableStream) {
    // Do something with each chunk
    // Here we just log the values
    log(`chunk: ${chunk}`);
  }
}

Result

The output of consuming the stream is shown below (if ReadableStream.from() is supported).

Convert an Array to a ReadableStream

This example demonstrates how you can convert an Array to a ReadableStream.

JavaScript

The iterable is just an array of strings that is passed to ReadableStream.from() to create the ReadableStream.

js
// An Array of vegetable names
const vegetables = ["Carrot", "Broccoli", "Tomato", "Spinach"];

// Create ReadableStream from the Array
const myReadableStream = ReadableStream.from(vegetables);

We use the same approach as in the previous example log and to consume the stream, so that is not shown here.

Result

The output is shown below.

Specifications

Specification
Streams
# rs-from

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
from() static method
Experimental
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Full support
Firefox 117 (Release date: 2023-08-29)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – Full support
Firefox for Android 117 (Release date: 2023-08-29)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
Deno – Full support
Deno 1.35 (Release date: 2023-07-05)
footnote Full support
Node.js – Full support
Node.js 20.6 (Release date: 2023-09-04)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

See also