Course contentsShow
← JavaScript
Lesson 2292 of 2,72347. Networking PrimitivesFree lesson

Getting a Reader from the Stream

Call getReader() on a ReadableStream to obtain a reader that can pull chunks of data incrementally.

Getting a Reader from the Stream

What you'll learn: How to obtain a reader from a ReadableStream so you can manually pull data chunks one at a time.

Why You Need a Reader

When you access a response's ReadableStream (via response.body), you have the stream itself, but you can't directly read from it. Think of the stream like a water pipe—you need a faucet to actually control the flow. That faucet is the reader.

Obtaining the Reader

Call getReader() on your ReadableStream to get a reader instance:

const response = await fetch('https://api.example.com/data');
const stream = response.body; // ReadableStream
const reader = stream.getReader(); // Now you have a reader!

What the Reader Does

The reader gives you exclusive access to pull chunks from the stream. Once you call getReader(), that stream is "locked" to that reader—no other code can read from it simultaneously. This prevents conflicts when processing streaming data.

The reader object has methods that let you:

  • Read chunks one at a time
  • Control when to pull more data
  • Cancel the stream if needed

When You'd Use This

Getting a reader is the first step when you want granular control over streaming data—processing large files incrementally, building progress indicators, or implementing custom parsing logic that doesn't wait for the entire response.

const response = await fetch('https://example.com/large-file');
const reader = response.body.getReader();
// Now you're ready to read chunks manually

Key Takeaway: Call getReader() on a ReadableStream to obtain a reader object that gives you exclusive, manual control over pulling data chunks from the stream.