ReadableStream
Creates a new ReadableStream that wraps an underlying data source and provides a standard interface for reading from it
Syntax
new ReadableStream(underlyingSource?, strategy?)Parameters
| Parameter | Type | Description |
|---|---|---|
| underlyingSource | UnderlyingSource | An object with start, pull, and cancel methods |
| strategy | QueuingStrategy | An object that defines the queuing strategy |
Return Value
A new ReadableStream instance
Examples
const stream = new ReadableStream({
start(controller) {
controller.enqueue('Hello')
controller.enqueue(' World')
controller.close()
}
})async function streamToString(stream: ReadableStream<Uint8Array>) {
const reader = stream.getReader()
const decoder = new TextDecoder()
let result = ''
while (true) {
const { done, value } = await reader.read()
if (done) break
result += decoder.decode(value, { stream: true })
}
return result
}const response = await fetch('/api/stream')
const reader = response.body!.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) break
console.log('Chunk:', new TextDecoder().decode(value))
}Understanding ReadableStream
The ReadableStream method in JavaScript creates a new ReadableStream that wraps an underlying data source and provides a standard interface for reading from it. It belongs to the ReadableStream object and is one of the most widely used methods for working with readablestream values in modern JavaScript and TypeScript applications.
The method signature is new ReadableStream(underlyingSource?, strategy?). It accepts 2 parameters: underlyingSource, strategy. When called, it returns a new readablestream instance. Understanding when and how to use ReadableStream() helps you write more expressive, readable code.
Common use cases for ReadableStream include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like fetch-response-json, fetch-response-text, fetch-response-blob, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for ReadableStream is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Response.prototype.jsonTakes a Response stream and reads it to completion, parsing the result as JSON
Response.prototype.textTakes a Response stream and reads it to completion, returning the result as a string
Response.prototype.blobTakes a Response stream and reads it to completion, returning the result as a Blob
Related Tools
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.