--- title: ReadableStreamDefaultReader slug: Web/API/ReadableStreamDefaultReader translation_of: Web/API/ReadableStreamDefaultReader ---
{{APIRef("Streams")}}{{SeeCompatTable}}
Streams API 的 ReadableStreamDefaultReader 的接口 表示一个可被用于读取来自网络提供的流数据(例如 fetch 请求)的默认读取器
ReadableStreamDefaultReader()
ReadableStreamDefaultReader()
对象实例.ReadableStreamDefaultReader.closed
ReadableStreamDefaultReader.cancel()
ReadableStreamDefaultReader.read()
ReadableStreamDefaultReader.releaseLock()
在下面的例子中, {{domxref("Response")}} 被创建为流 HTML片段 fetched 来自其他源.
它展示了一个 {{domxref("ReadableStream")}} 和一个 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 |
---|---|---|
{{SpecName('Streams','#default-reader-class','ReadableStreamDefaultReader')}} | {{Spec2('Streams')}} | 初始定义 |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support |
{{CompatChrome(52.0)}} |
{{CompatGeckoDesktop("57.0")}} | {{CompatUnknown}} |
{{CompatOpera(39)}} |
{{CompatUnknown}} |
Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatChrome(52)}} | {{CompatChrome(52)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatOperaMobile(39)}} | {{CompatUnknown}} |