This community-built FAQ covers the “Readable Streams” exercise from the lesson “Learn Node.js”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Learn Node.js
FAQs on the exercise Readable Streams
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply ( ) below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply ( ) below!
Agree with a comment or answer? Like ( ) to up-vote the contribution!
Need broader help or resources ? Head here .
Looking for motivation to keep learning? Join our wider discussions .
Learn more about how to use this guide.
Found a bug ? Report it!
Have a question about your account or billing? Reach out to our customer support team !
None of the above? Find out where to ask other questions here !
In this example:
{
const readline = require(‘readline’);
const fs = require(‘fs’);
const myInterface = readline.createInterface({
input: fs.createReadStream(‘shoppingList.txt’)
});
const printData = (data) => {
console.log(Item: ${data}
);
}
myInterface.on(‘line’, printData);
}
What would the code have to look like to have the output be:
‘Item [numberOfItem]: [item]’
Example:
‘Item 1: Bananas
Item 2: Apples
Item 3: Chicken
Item 3: Broccoli
.
.’
Could we accomplish this by nesting the printData function in a for…loop?
Or a for…loop within the printData function?
Thank you for any of your ideas, responses, help😊
So, I actually figured it out myself!
const readline = require(‘readline’);
const fs = require(‘fs’);
const myInterface = readline.createInterface({
input: fs.createReadStream(’./groc.txt’)
})
let nums = 0;
const printData = (data) => {
console.log(Item ${nums.toString()}:
)
console.log( ${data}
)
}
myInterface.on(‘line’, function() {
nums+=1;
})
myInterface.on(‘line’, printData);
Yet I wonder if perhaps there is a more “efficient” way of achieving this?
const readline = require('readline');
const fs = require('fs');
const myInterface=readline.createInterface({input: fs.createReadStream('shoppingList.txt')});
let num=0;
const printData=(data)=>{
console.log(`Item ${++num}: ${data}`)
}
myInterface.on('line',printData);
This way it is more simpler.
shiqihe
December 10, 2020, 3:34am
6
Hi, I have question about the sequence of the code.
Here is the solution:
let settings = {
input: fs.createReadStream('shoppingList.txt')
};
const myInterface = readline.createInterface(settings);
const printData = (data) => {
console.log(`Item: ${data}`);
};
myInterface.on('line', printData);
.createInterface()
returns an EventEmitter
sets up to emit 'line'
events, so we raise the events when we call the readline.createInterface()
.
We register our listener myInterface.on()
after we raise the event. But the program works. Can someone explain it to me? Thanks for your help!
On Step 2 the solution seems to require an ES6-style arrow function - is there a reason for this?
For those looking for a more concise solution to the exercise, I wrote it like this:
const readline = require('readline');
const fs = require('fs');
const myInterface = readline.createInterface({
input: fs.createReadStream('shoppingList.txt')
});
myInterface.on('line', (printData = (data) => {
console.log(`item: ${data}`);
}));
1 Like
Hi! I have a doubt about .on() method: Isn’t it supposed to be a method that belongs to the ‘events’ core module? In the current exercise, it is used on a ‘readline’ core module.
const readline = require(‘readline’);
const fs = require(‘fs’);
const myInterface = readline.createInterface ({
input: fs.createReadStream(’./shoppingList.txt’)
});
const printData = data => {
console.log(Item: ${data}
)
};
myInterface.on (‘line’, printData);