Complete the code to import the Readable stream module.
const { [1] } = require('stream');The Readable class is imported from the stream module to create readable streams.
Complete the code to create a readable stream from an array of strings.
const readable = Readable.from([1]);
The Readable.from() method creates a readable stream from an iterable like an array of strings.
Fix the error in the code to read data chunks from the stream.
readable.on('data', ([1]) => { console.log(chunk.toString()); });
The callback parameter name must match the variable used inside the function. Here, chunk is used to log the data.
Fill both blanks to correctly handle the end of the readable stream.
readable.on('[1]', () => { console.log('[2]'); });
The end event signals that no more data will be provided by the stream. Logging a message inside this event handler informs the user.
Fill all three blanks to create a readable stream and log all data chunks and the end event.
const { [1] } = require('stream');
const data = ['Node', 'JS', 'Streams'];
const readable = [2].from(data);
readable.on('data', ([3]) => {
console.log([3].toString());
});First, import Readable. Then create a readable stream from the array data using Readable.from(). Finally, listen to the 'data' event with a parameter chunk to log each data piece.