Challenge - 5 Problems
Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat does this ReadableStream output when read?
Consider this Deno code creating a ReadableStream that enqueues numbers 1 to 3. What will be the output when reading all chunks?
Deno
const stream = new ReadableStream({
start(controller) {
controller.enqueue(1);
controller.enqueue(2);
controller.enqueue(3);
controller.close();
}
});
const reader = stream.getReader();
async function readAll() {
const results = [];
while (true) {
const { done, value } = await reader.read();
if (done) break;
results.push(value);
}
return results;
}
readAll().then(console.log);Attempts:
2 left
💡 Hint
Think about what the controller.enqueue method does and when the stream closes.
✗ Incorrect
The stream enqueues 1, 2, and 3 in order, then closes. The reader reads all chunks until done is true, so the output is [1, 2, 3].
📝 Syntax
intermediateWhich option correctly creates a WritableStream that logs each chunk?
You want to create a WritableStream in Deno that logs every chunk it receives. Which code snippet is correct?
Attempts:
2 left
💡 Hint
WritableStream write method can be async and must be a function property.
✗ Incorrect
Option A correctly defines an async write function. Option A is missing async keyword (allowed but less common). Option A uses await incorrectly on console.log (which is not a promise). Option A uses arrow function but write expects a method, not arrow function.
❓ state_output
advancedWhat is the final value of 'count' after piping a ReadableStream to a WritableStream?
Given this code, what is the value of count after the pipe operation completes?
Deno
let count = 0; const readable = new ReadableStream({ start(controller) { for (let i = 0; i < 5; i++) { controller.enqueue(i); } controller.close(); } }); const writable = new WritableStream({ write(chunk) { count += chunk; } }); await readable.pipeTo(writable); console.log(count);
Attempts:
2 left
💡 Hint
Sum numbers from 0 to 4.
✗ Incorrect
The readable stream enqueues 0,1,2,3,4. The writable stream adds each chunk to count. Sum 0+1+2+3+4 = 10.
🔧 Debug
advancedWhy does this WritableStream code cause a runtime error?
This WritableStream code throws an error when writing chunks. What is the cause?
Deno
const writable = new WritableStream({
write(chunk) {
this.total += chunk;
},
total: 0
});
const writer = writable.getWriter();
await writer.write(5);Attempts:
2 left
💡 Hint
Check what 'this' refers to inside the write method.
✗ Incorrect
Inside the write method, 'this' is undefined because the method is called without binding. The property 'total' is not accessible, causing a TypeError.
🧠 Conceptual
expertWhich statement about backpressure in streams is correct?
In the context of ReadableStream and WritableStream in Deno, which statement about backpressure is true?
Attempts:
2 left
💡 Hint
Think about how streams coordinate data flow to avoid overload.
✗ Incorrect
Backpressure is a mechanism where the writable stream signals it is busy, causing the readable stream to slow or pause to avoid overwhelming the writable stream.
