The enqueue()
method of the ReadableStreamDefaultController
interface enqueues a given chunk in the associated stream.
The enqueue()
method of the ReadableStreamDefaultController
interface enqueues a given chunk in the associated stream.
js
enqueue(chunk)
chunk
The chunk to enqueue.
None (undefined
).
TypeError
Thrown if the source object is not a ReadableStreamDefaultController
.
In the following simple example, a custom ReadableStream
is created using a constructor (see our 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 — see controller.enqueue(string)
. 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.
js
let interval; 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", () => { 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); }, });
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
enqueue |
≤80 | 80 | 65 | No | 67 | ≤13.1 | 80 | 80 | 65 | 57 | ≤13.4 | 13.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/enqueue