Discover how streaming data can make your apps faster and more reliable without breaking a sweat!
Why Streaming with ReadableStream and WritableStream in Deno? - Purpose & Use Cases
Imagine you want to process a huge file line by line in your program. You try to load the entire file into memory at once before working on it.
Loading the whole file at once can crash your program if the file is too big. It also wastes time and memory because you wait until everything is ready before starting to work.
Using ReadableStream and WritableStream lets you handle data piece by piece as it arrives. This way, your program can start working immediately and use memory efficiently.
const data = await Deno.readFile('bigfile.txt'); process(data);const file = await Deno.open('bigfile.txt'); for await (const chunk of file.readable) { process(chunk); }
It enables smooth, memory-friendly processing of large or continuous data without waiting for everything to load.
Streaming video or audio files where data flows continuously and your app plays it without delays or crashes.
Manual loading of large data is slow and risky.
ReadableStream and WritableStream handle data in small parts.
This approach improves performance and user experience.
Practice
ReadableStream in Deno?Solution
Step 1: Understand the role of ReadableStream
AReadableStreamis designed to produce data chunks that can be read by consumers.Step 2: Differentiate from WritableStream
AWritableStreamaccepts data chunks to be written, which is the opposite role.Final Answer:
To produce data chunks that can be read -> Option BQuick Check:
ReadableStream produces data = A [OK]
- Confusing ReadableStream with WritableStream
- Thinking streams store data permanently
- Assuming streams convert data formats automatically
WritableStream in Deno?Solution
Step 1: Recall WritableStream constructor syntax
WritableStream is created withnew WritableStream()and an object with awritemethod.Step 2: Check each option's syntax
const stream = new WritableStream({ write(chunk) { console.log(chunk); } }); correctly usesnew WritableStream({ write(chunk) { ... } }). Others misuse syntax or omitnew.Final Answer:
const stream = new WritableStream({ write(chunk) { console.log(chunk); } }); -> Option AQuick Check:
WritableStream needs new + write method object = C [OK]
- Omitting 'new' keyword
- Calling WritableStream as a function
- Passing a function directly instead of an object
const readable = new ReadableStream({
start(controller) {
controller.enqueue('Hello');
controller.enqueue('World');
controller.close();
}
});
const reader = readable.getReader();
(async () => {
let result = '';
while(true) {
const { done, value } = await reader.read();
if (done) break;
result += value + ' ';
}
console.log(result.trim());
})();Solution
Step 1: Understand the ReadableStream setup
The stream enqueues two chunks: 'Hello' and 'World', then closes.Step 2: Analyze the reading loop
The loop reads each chunk, appends it with a space toresult, and stops when done is true.Step 3: Output after trimming
After reading both chunks,resultis 'Hello World ' which trims to 'Hello World'.Final Answer:
Hello World -> Option CQuick Check:
Chunks read and concatenated with space = D [OK]
- Forgetting to await reader.read()
- Not trimming the final string
- Assuming chunks concatenate without spaces
const writable = new WritableStream({
write(chunk) {
console.log(chunk.toUpperCase());
}
});
const writer = writable.getWriter();
await writer.write(123);
await writer.close();Solution
Step 1: Check the write method usage
The write method callschunk.toUpperCase(), which only works on strings.Step 2: Analyze the write call argument
The code writes the number 123, which has notoUpperCase()method, causing a runtime error.Final Answer:
Cannot call toUpperCase on a number -> Option DQuick Check:
toUpperCase on number causes error = A [OK]
- Assuming write must be async
- Thinking getWriter() is invalid
- Missing await on WritableStream creation
ReadableStream of text chunks to uppercase and write them to a WritableStream. Which approach correctly implements this in Deno?Solution
Step 1: Understand stream transformation
To modify data between readable and writable, use aTransformStreamthat changes chunks to uppercase.Step 2: Use pipeThrough and pipeTo methods
Pipe the readable stream through the transform, then pipe the result to the writable stream for correct flow and backpressure handling.Step 3: Evaluate other options
Directly write chunks from readable to writable without transformation skips transformation, C risks locking issues, and D transforms too late after writing.Final Answer:
Pipe the readable through a TransformStream that uppercases chunks, then pipe to writable -> Option AQuick Check:
Use TransformStream + pipeThrough + pipeTo = B [OK]
- Skipping TransformStream for data modification
- Not handling stream locks properly
- Transforming data after writing
Ready to prove it?
Pass the test and this pattern moves unknown
