I am trying to keep adding numbers in a loop as shown in the code below:
const readline = require('readline');
const askOnCommandLine = (question) =>
new Promise((resolve) => {
const p = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: question,
});
p.on('line', (input) => {
resolve(input);
});
p.prompt();
});
let counter = 0;
(async () => {
while (true) {
console.log('Loop:', counter);
const number1 = await askOnCommandLine('Enter 1st number: ');
const number2 = await askOnCommandLine('Enter 2nd number: ');
console.log('Addition:', parseInt(number1) + parseInt(number2));
counter += 1;
}
})();
But there are two problems here:
- It prints a single key press multiple times as shown in the screenshot.
- After a few loops, there an error as follows:
Enter 2nd number:
(node:706492) MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
11 end listeners added to [ReadStream]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)