Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. ReadableStream
  4. getReader()

ReadableStream: getReader() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨January 2019⁩.

Note: This feature is available inWeb Workers.

ThegetReader() method of theReadableStream interface 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.

Syntax

js
getReader()getReader(options)

Parameters

optionsOptional

An object containing the following properties:

modeOptional

A property that specifies the type of reader to create.Values can be:

  • "byob", which results in aReadableStreamBYOBReader being created that can read readable byte streams (streams that support zero-copy transfer from an underlying byte source to the reader when internal stream buffers are empty).
  • undefined (or not specified at all — this is the default), which results in aReadableStreamDefaultReader being created that can read individual chunks from a stream.

Return value

AReadableStreamDefaultReader orReadableStreamBYOBReader object instance, depending on themode value.

Exceptions

RangeError

Thrown if the provided mode value is not"byob" orundefined.

TypeError

Thrown if the stream you are trying to create a reader for is already locked, or not aReadableStream.This is also thrown if a BYOB reader is requested and the stream controller is not aReadableByteStreamController (the stream was notconstructed as an underlying source withtype="bytes").

Examples

In the following simple example, a previously-created customReadableStream is read using aReadableStreamDefaultReader created usinggetReader().(see ourSimple random stream example for the full code).Each chunk is read sequentially and output to the UI, until the stream has finished being read, at which point we return out of the recursive function and print the entire stream to another part of the UI.

js
function fetchStream() {  const reader = stream.getReader();  let charsReceived = 0;  // read() returns a promise that resolves  // when a value has been received  reader.read().then(function processText({ done, value }) {    // Result objects contain two properties:    // done  - true if the stream has already given you all its data.    // value - some data. Always undefined when done is true.    if (done) {      console.log("Stream complete");      para.textContent = value;      return;    }    // value for fetch streams is a Uint8Array    charsReceived += value.length;    const chunk = value;    let listItem = document.createElement("li");    listItem.textContent = `Received ${charsReceived} characters so far. Current chunk = ${chunk}`;    list2.appendChild(listItem);    result += chunk;    // Read some more, and call this function again    return reader.read().then(processText);  });}

Specifications

Specification
Streams
# ref-for-rs-get-reader⑤

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp