This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The ReadableStreamDefaultReader
interface of the Streams API represents a default reader that can be used to read stream data supplied from a network (e.g. a fetch request).
ReadableStreamDefaultReader()
ReadableStreamDefaultReader()
object instance.ReadableStreamDefaultReader.closed
ReadableStreamDefaultReader.cancel()
ReadableStreamDefaultReader.read()
ReadableStreamDefaultReader.releaseLock()
In the following example, an artifical 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) => { const reader = response.body.getReader(); const stream = new ReadableStream({ start(controller) { // The following function handles each data chunk function push() { // "done" is a Boolean and value a "Uint8Array" return reader.read().then(({ done, value }) => { // Is there no more data to read? if (done) { // Tell the browser that we have finished sending data controller.close(); return; } // Get the data and send it to the browser via the controller controller.enqueue(value); }).then(push); }; push(); } }); return new Response(stream, { headers: { "Content-Type": "text/html" } }); });
Specification | Status | Comment |
---|---|---|
Streams The definition of 'ReadableStreamDefaultReader' in that specification. | Living Standard | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 52 | ? | 57 | ? | 39 | ? |
ReadableStreamDefaultReader() constructor
|
? | ? | ? | ? | ? | ? |
cancel
|
? | ? | ? | ? | ? | ? |
closed
|
? | ? | ? | ? | ? | ? |
read
|
? | ? | ? | ? | ? | ? |
releaseLock
|
? | ? | ? | ? | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 52 | 52 | ? | ? | 39 | ? | ? |
ReadableStreamDefaultReader() constructor
|
? | ? | ? | ? | ? | ? | ? |
cancel
|
? | ? | ? | ? | ? | ? | ? |
closed
|
? | ? | ? | ? | ? | ? | ? |
read
|
? | ? | ? | ? | ? | ? | ? |
releaseLock
|
? | ? | ? | ? | ? | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader