ReadableStream
The ReadableStream
interface of the Streams API represents a readable stream of byte data. The Fetch API offers a concrete instance of a ReadableStream
through the body
property of a Response
object.
Constructor
ReadableStream()
-
Creates and returns a readable stream object from the given handlers.
Properties
-
ReadableStream.locked
Read only -
The
locked
getter returns whether or not the readable stream is locked to a reader.
Methods
ReadableStream.cancel()
-
Returns a
Promise
that resolves when the stream is canceled. Calling this method signals a loss of interest in the stream by a consumer. The suppliedreason
argument will be given to the underlying source, which may or may not use it. ReadableStream.getReader()
-
Creates a reader and locks the stream to it. While the stream is locked, no other reader can be acquired until this one is released.
ReadableStream.pipeThrough()
-
Provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.
ReadableStream.pipeTo()
-
Pipes the current ReadableStream to a given
WritableStream
and returns aPromise
that fulfills when the piping process completes successfully, or rejects if any errors were encountered. ReadableStream.tee()
-
The
tee
method tees this readable stream, returning a two-element array containing the two resulting branches as newReadableStream
instances. Each of those streams receives the same incoming data.
Examples
Fetch stream
In the following example, an artificial Response
is created to stream HTML fragments fetched from another resource to the browser.
It demonstrates the usage of a ReadableStream
in combination with a Uint8Array
.
fetch('https://www.example.org') .then(response => response.body) .then(rb => { const reader = rb.getReader(); return new ReadableStream({ start(controller) { // The following function handles each data chunk function push() { // "done" is a Boolean and value a "Uint8Array" reader.read().then( ({done, value}) => { // If there is no more data to read if (done) { console.log('done', done); controller.close(); return; } // Get the data and send it to the browser via the controller controller.enqueue(value); // Check chunks by logging to the console console.log(done, value); push(); }) } push(); } }); }) .then(stream => { // Respond with our stream return new Response(stream, { headers: { "Content-Type": "text/html" } }).text(); }) .then(result => { // Do things with result console.log(result); });
Async iterator to stream
Converting an (async) iterator to a readable stream:
function iteratorToStream(iterator) { return new ReadableStream({ async pull(controller) { const { value, done } = await iterator.next(); if (done) { controller.close(); } else { controller.enqueue(value); } }, }); }
This works with both async and non-async iterators.
Specifications
Specification |
---|
Streams Standard (Streams) # rs-class |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
ReadableStream |
43 |
14 |
65 |
No |
30 |
10.1 |
43 |
43 |
65 |
30 |
10.3 |
4.0 |
ReadableStream |
43 |
79 |
65 |
No |
30 |
10.1 |
43 |
43 |
65 |
30 |
10.3 |
4.0 |
cancel |
43 |
14 |
65 |
No |
30 |
10.1 |
43 |
43 |
65 |
30 |
10.3 |
4.0 |
getReader |
43 |
14 |
65 |
No |
30 |
10.1 |
43 |
43 |
65 |
30 |
10.3 |
4.0 |
locked |
43 |
14 |
65 |
No |
30 |
10.1 |
43 |
43 |
65 |
30 |
10.3 |
4.0 |
pipeThrough |
59 |
79 |
No |
No |
46 |
10.1 |
59 |
59 |
No |
43 |
10.3 |
7.0 |
pipeTo |
59 |
79 |
No |
No |
46 |
10.1 |
59 |
59 |
No |
43 |
10.3 |
7.0 |
tee |
43 |
79 |
65 |
No |
30 |
10.1 |
43 |
43 |
65 |
30 |
10.3 |
4.0 |
See also
- WHATWG Stream Visualiser, for a basic visualisation of readable, writable, and transform streams.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream