ReadableStreamDefaultReader

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()
Creates and returns a ReadableStreamDefaultReader object instance.

特性

ReadableStreamDefaultReader.closed 只读
Allows you to write code that responds to an end to the streaming process. Returns a promise that fulfills if the stream becomes closed or the reader's lock is released, or rejects if the stream errors.

方法

ReadableStreamDefaultReader.cancel()
Cancels the stream, signaling a loss of interest in the stream by a consumer. The supplied reason argument will be given to the underlying source, which may or may not use it.
ReadableStreamDefaultReader.read()
Returns a promise providing access to the next chunk in the stream's internal queue.
ReadableStreamDefaultReader.releaseLock()
Releases the reader's lock on the stream.

范例

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);
          push();
        });
      };
      push();
    }
  });
  return new Response(stream, { headers: { "Content-Type": "text/html" } });
});
					

规范

规范 状态 Comment

The definition of 'ReadableStreamDefaultReader' in that specification.
实时标准 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ReadableStreamDefaultReader
Chrome 52 Edge ≤79 Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE ? Opera 39 Safari ? WebView Android 52 Chrome Android 52 Firefox Android 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
ReadableStreamDefaultReader() 构造函数
Chrome ? Edge ? Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS ? Samsung Internet Android ?
cancel
Chrome ? Edge ? Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS ? Samsung Internet Android ?
closed
Chrome ? Edge ? Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS ? Samsung Internet Android ?
read
Chrome ? Edge ? Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS ? Samsung Internet Android ?
releaseLock
Chrome ? Edge ? Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS ? Samsung Internet Android ?

图例

完整支持
完整支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
用户必须明确启用此特征。
用户必须明确启用此特征。