/Web APIs


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.

ReadableStream is a transferable object.



Creates and returns a readable stream object from the given handlers.

Instance properties

ReadableStream.locked Read only

Returns a boolean indicating whether or not the readable stream is locked to a reader.

Instance methods


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 supplied reason argument will be given to the underlying source, which may or may not use it.


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.


Provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.


Pipes the current ReadableStream to a given WritableStream and returns a Promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered.


The tee method tees this readable stream, returning a two-element array containing the two resulting branches as new ReadableStream instances. Each of those streams receives the same incoming data.

Async iteration

ReadableStream implements the async iterable protocol. This enables asynchronous iteration over the chunks in a stream using the for await...of syntax:

const stream = new ReadableStream(getSomeSource());

for await (const chunk of stream) {
  // Do something with each 'chunk'

The async iterator consumes the stream until it runs out of data or otherwise terminates. The loop can also exit early due to a break, throw, or return statement.

While iterating, the stream is locked to prevent other consumers from acquiring a reader (attempting to iterate over a stream that is already locked will throw a TypeError). This lock is released when the loop exits.

By default, exiting the loop will also cancel the stream, so that it can no longer be used. To continue to use a stream after exiting the loop, pass { preventCancel: true } to the stream's values() method:

for await (const chunk of stream.values({ preventCancel: true })) {
  // Do something with 'chunk'
// Acquire a reader for the stream and continue reading ...


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.

  .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);
            // Get the data and send it to the browser via the controller
            // Check chunks by logging to the console
            console.log(done, value);

  .then((stream) =>
    // Respond with our stream
    new Response(stream, { headers: { "Content-Type": "text/html" } }).text()
  .then((result) => {
    // Do things with result

Convert 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) {
      } else {

This works with both async and non-async iterators.

Async iteration of a stream using for await...of

This example shows how you can process the fetch() response using a for await...of loop to iterate through the arriving chunks.

const response = await fetch("https://www.example.org");
let total = 0;

// Iterate response.body (a ReadableStream) asynchronously
for await (const chunk of response.body) {
  // Do something with each chunk
  // Here we just accumulate the size of the response.
  total += chunk.length;

// Do something with the total


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
@@asyncIterator No No 110 No No No No No 110 No No No
ReadableStream 52 79 65 No 39 10.1 52 52 65 41 10.3 6.0
ReadableStream 43 14 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 52 14 65 No 39 10.1 52 52 65 41 10.3 6.0
pipeThrough 59 79 102 No 46 10.1 59 59 102 43 10.3 7.0
pipeTo 59 79 100 No 46 10.1 59 59 100 43 10.3 7.0
tee 52 79 65 No 39 10.1 52 52 65 41 10.3 6.0
transferable 87 87 103 No 73 No 87 87 103 62 No 14.0
values No No 110 No No No No No 110 No No No

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.