Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a Readable stream in Node.js?
A Readable stream is a source of data that you can read from in chunks, like reading a file or receiving data over the network. It lets you process data piece by piece instead of loading it all at once.
Click to reveal answer
beginner
How do you start reading data from a Readable stream in Node.js?
You can listen to the 'data' event on the stream to get chunks of data as they arrive. For example: stream.on('data', chunk => { /* use chunk */ });
Click to reveal answer
beginner
What does the 'end' event on a Readable stream signify?
The 'end' event means the stream has no more data to provide. It tells you that reading is finished.
Click to reveal answer
intermediate
What is the difference between flowing mode and paused mode in Readable streams?
In flowing mode, data is read automatically and emitted via 'data' events. In paused mode, you must call stream.read() to get data manually.
Click to reveal answer
intermediate
How can you read all data from a Readable stream using async/await?
You can use a for-await-of loop to read chunks asynchronously: for await (const chunk of stream) { /* process chunk */ }
Click to reveal answer
Which event do you listen to for receiving chunks of data from a Readable stream?
A'data'
B'end'
C'error'
D'close'
✗ Incorrect
The 'data' event is emitted whenever a chunk of data is available to read.
What does the 'end' event on a Readable stream indicate?
AAn error occurred
BData is flowing
CThe stream is paused
DThe stream has no more data
✗ Incorrect
The 'end' event signals that the stream has finished sending all data.
How do you switch a Readable stream to flowing mode?
ACall stream.pause()
BCall stream.resume() or add a 'data' event listener
CCall stream.read() repeatedly
DCall stream.close()
✗ Incorrect
Adding a 'data' event listener or calling stream.resume() makes the stream flow automatically.
Which syntax allows reading data from a Readable stream using async/await?
Afor await (const chunk of stream) { }
Bstream.on('data', chunk => { })
Cstream.read()
Dstream.pipe()
✗ Incorrect
The for-await-of loop lets you asynchronously iterate over chunks from the stream.
What happens if you do not listen to 'data' or call stream.read() on a Readable stream?
AThe stream will emit data anyway
BThe stream will close immediately
CThe stream will stay in paused mode and not emit data
DThe stream will throw an error
✗ Incorrect
Without reading, the stream stays paused and does not emit data chunks.
Explain how to read data from a Readable stream in Node.js using event listeners.
Think about how you get pieces of data and know when the stream finishes.
You got /3 concepts.
Describe the difference between flowing and paused modes in Readable streams and how to switch between them.
Consider how the stream sends data and how you control it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Readable stream in Node.js?
easy
A. To create a new HTTP server
B. To read data piece by piece without loading it all at once
C. To execute JavaScript code asynchronously
D. To write data to a file all at once
Solution
Step 1: Understand what a Readable stream does
A Readable stream reads data in small chunks instead of loading everything at once, which helps save memory.
Step 2: Compare options with the purpose
Only To read data piece by piece without loading it all at once describes reading data piece by piece, which matches the purpose of Readable streams.
Final Answer:
To read data piece by piece without loading it all at once -> Option B
Quick Check:
Readable stream = read data in chunks [OK]
Hint: Readable streams read data bit by bit, not all at once [OK]
Common Mistakes:
Confusing Readable streams with writing data
Thinking streams execute code
Mixing streams with server creation
2. Which of the following is the correct way to listen for data chunks from a Readable stream named stream?
easy
A. stream.on('data', chunk => { console.log(chunk); });
B. stream.emit('data', chunk => { console.log(chunk); });
C. stream.read('data', chunk => { console.log(chunk); });
D. stream.listen('data', chunk => { console.log(chunk); });
Solution
Step 1: Recall the event listening method in Node.js streams
To listen for events, use the on method with the event name and a callback function.
Step 2: Match the correct syntax
stream.on('data', chunk => { console.log(chunk); }); uses stream.on('data', callback), which is the correct way to listen for data chunks.
Final Answer:
stream.on('data', chunk => { console.log(chunk); }); -> Option A
Quick Check:
Use .on() to listen to stream events [OK]
Hint: Use .on('data', callback) to read chunks from streams [OK]
Common Mistakes:
Using emit instead of on to listen
Using non-existent methods like read or listen
Confusing event listening with emitting
3. What will the following code output if the file example.txt contains the text "Hello World"?
A. The 'data' event callback is missing a parameter
B. The stream should be paused before reading
C. The 'finish' event does not exist on Readable streams
D. The file path must be absolute
Solution
Step 1: Check event names for Readable streams
Readable streams emit 'end' when done, not 'finish'. 'finish' is for Writable streams.
Step 2: Identify the incorrect event usage
The code listens for 'finish', which will never fire on a Readable stream, causing the message to never appear.
Final Answer:
The 'finish' event does not exist on Readable streams -> Option C
Quick Check:
Readable streams use 'end', not 'finish' event [OK]
Hint: 'finish' is for writing; use 'end' for reading streams [OK]
Common Mistakes:
Using 'finish' event on Readable streams
Forgetting to handle 'end' event
Assuming 'data' event has no parameters
5. You want to read a large file using a Readable stream and count how many times the word "node" appears, case-insensitive. Which approach correctly handles data chunks to count occurrences without missing matches across chunk boundaries?
hard
A. Convert each chunk to string and count occurrences immediately, ignoring chunk edges
B. Count occurrences in each chunk separately without storing leftover text
C. Use a writable stream instead of a readable stream for counting
D. Concatenate chunks into a string, then count occurrences after 'end' event
Solution
Step 1: Understand chunk boundaries in streams
Chunks may split words, so counting in each chunk separately can miss matches that cross chunk edges.
Step 2: Choose a method to avoid missing matches
Concatenating all chunks into one string and counting after the 'end' event ensures all occurrences are counted correctly.
Final Answer:
Concatenate chunks into a string, then count occurrences after 'end' event -> Option D
Quick Check:
Count after full data read to avoid split word misses [OK]
Hint: Join chunks first, then count words to avoid split matches [OK]