Web/API/ReadableStreamDefaultController/close

From Get docs


The close() method of the ReadableStreamDefaultController interface closes the associated stream.

Readers will still be able to read any previously-enqueued chunks from the stream, but once those are read, the stream will become closed. If you want to completely get rid of the stream and discard any enqueued chunks, you'd use ReadableStream.cancel() or ReadableStreamDefaultReader.cancel().


Syntax

readableStreamDefaultController.close();

Parameters

None.

Return value

undefined.

Exceptions

TypeError
The source object is not a ReadableStreamDefaultController.

Examples

In the following simple example, a custom ReadableStream is created using a constructor (see our [[../../../../../../../mdn.github.io/dom-examples/streams/simple-random-stream/index|Simple random stream example]] for the full code). The start() function generates a random string of text every second and enqueues it into the stream. A cancel() function is also provided to stop the generation if ReadableStream.cancel() is called for any reason.

When a button is pressed, the generation is stopped, the stream is closed using ReadableStreamDefaultController.close(), and another function is run, which reads the data back out of the stream.

const stream = new ReadableStream({
  start(controller) {
    interval = setInterval(() => {
      let string = randomChars();

      // Add the string to the stream
      controller.enqueue(string);

      // show it on the screen
      let listItem = document.createElement('li');
      listItem.textContent = string;
      list1.appendChild(listItem);
    }, 1000);

    button.addEventListener('click', function() {
      clearInterval(interval);
      fetchStream();
      controller.close();
    })
  },
  pull(controller) {
    // We don't really need a pull in this example
  },
  cancel() {
    // This is called if the reader cancels,
    // so we should stop generating strings
    clearInterval(interval);
  }
});

Specifications

Specification Status Comment
StreamsThe definition of 'close()' in that specification. Living Standard Initial definition.

Browser compatibility

Update compatibility data on 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

close

Experimental'

Chrome

?

Edge

?

Firefox Full support 65


Full support 65


Full support 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

No support No

Opera

?

Safari

?

WebView Android

?

Chrome Android

?

Firefox Android Full support 65


Full support 65


Full support 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

?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.'
User must explicitly enable this feature.