Challenge - 5 Problems
Readable Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Node.js Readable stream code?
Consider the following code that reads data from a Readable stream. What will be logged to the console?
Node.js
import { Readable } from 'stream'; const readable = Readable.from(['Hello', ' ', 'World']); readable.on('data', chunk => { console.log(chunk.toString()); });
Attempts:
2 left
💡 Hint
Think about how the 'data' event emits chunks separately.
✗ Incorrect
The Readable stream emits each chunk separately on the 'data' event. Each chunk is logged on its own line, so the console shows each piece on a new line.
📝 Syntax
intermediate1:30remaining
Which option correctly creates a Readable stream from an array of strings?
You want to create a Readable stream from an array of strings ['a', 'b', 'c']. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the correct way to use Readable.from static method.
✗ Incorrect
Readable.from is a static method to create a stream from iterable data. It is called directly on Readable, not with new.
❓ state_output
advanced2:00remaining
What is the value of 'dataCollected' after this code runs?
This code reads data from a Readable stream and collects it into a string. What is the final value of 'dataCollected'?
Node.js
import { Readable } from 'stream'; const readable = Readable.from(['Node', 'JS']); let dataCollected = ''; readable.on('data', chunk => { dataCollected += chunk.toString(); }); readable.on('end', () => { // end event });
Attempts:
2 left
💡 Hint
Remember how the chunks are emitted and concatenated.
✗ Incorrect
The stream emits 'Node' and then 'JS' as chunks. Concatenating them results in 'NodeJS'.
🔧 Debug
advanced2:30remaining
Why does this Readable stream code cause a runtime error?
Examine the code below. Why does it throw an error when run?
Node.js
import { Readable } from 'stream'; const readable = new Readable({ read() { this.push('data'); this.push(null); } }); readable.on('data', chunk => { console.log(chunk.toString()); }); readable.on('end', () => { console.log('Stream ended'); });
Attempts:
2 left
💡 Hint
Consider how the Readable stream's read() method is used internally.
✗ Incorrect
Calling readable.read() manually when the stream is in flowing mode and pushing data inside read() causes an error because read() is meant to be called by the stream internally.
🧠 Conceptual
expert3:00remaining
Which option best explains how backpressure is handled in Node.js Readable streams?
Backpressure helps control data flow in streams. Which explanation correctly describes how Node.js Readable streams handle backpressure?
Attempts:
2 left
💡 Hint
Think about how streams avoid overwhelming the consumer.
✗ Incorrect
Node.js Readable streams manage backpressure by buffering data and pausing the flow of 'data' events when the consumer cannot keep up, then resuming when ready.