Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
read user input N times from console node js

I'm trying to get an input from a user to a console in node js a finite number of times, and run a function on each input. while or for loop doesn't work.

any help?

here is my code (litle simplified):

function foo(num)
{
 console.log(num)
}

function ReadUseInput()
{
 const readline = require('readline').createInterface({
     input: process.stdin,
    output: process.stdout
   }); 
    readline.question('Enter number...', num => 
  {
         foo(num)
       readline.close();
     });
}

//for (var i = 0; i < 10; i++)// this line isnt working - shows warning: MaxListenersExceededWarning: Possible EventEmitter memory leak detected
ReadUseInput()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One solution could be to make the ReadUseInput() function take a number and decrement it to repeat a certain number of times:

function foo(num) {
 console.log(num)
}

function ReadUseInput(timesLeft) {
  // exit condition
  if(timesLeft <= 0) {
    return;
  }
  const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
  }); 
  readline.question('Enter number...', num => {
    foo(num)
    readline.close();
    ReadUseInput(--timesLeft);
  });
}

ReadUseInput(10);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!