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

215
Views
node.js readline with if / else statements - gets stuck if empty input

I want to output questions and receive answers as input in a terminal. For each question, there is a check, in this case if the input was provided or not. If it is, next question should be outputted; if not, question should be looped.

If input is provided every time, resolve() gets called and moves on to the next question, as it should. But the problem happens if the first time an empty input is sent. It kinda gets stuck and the resolve() does not work any more

const rdline = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let userName, userSurname = "";

1st question

const question_name = () => {
    return new Promise((resolve) => {
        rdline.question("What's your name?\n", name => {
            if (name) {
                userName = name;
                resolve();
            } else {
                question_name();

            }
        });
    });

};

2nd question

const question_surname = () => {
    return new Promise((resolve) => {
        rdline.question("What's your surname?\n", surname => {
            if (surname) {
                userSurname = surname;
                resolve();
            } else {
                question_surname();
            }
        });
    });
};

call both questions

const askUser = async () => {
    await question_name();
    await question_surname();
    console.log(`Hi, ${userName} ${userSurname}`);
    rdline.close();
};

askUser();
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It seems you're not resolving the data and are expecting userName and userSurname to exist in askUser.

Instead resolve the answers, and the repeat questions, and assign them inside askUser:

// mock rdline
const rdline = {
  question: (a,b) => a.match(/surname/) ? b('Test') : b('User'),
  close: () => {}
};

const question_name = () => {
    return new Promise((resolve) => {
        rdline.question("What's your name?\n", name => {
            if (name) {
                resolve(name);
            } else {
                resolve(question_name());
            }
        });
    });

};

const question_surname = () => {
    return new Promise((resolve) => {
        rdline.question("What's your surname?\n", surname => {
            if (surname) {
                resolve(surname);
            } else {
                resolve(question_surname());
            }
        });
    });
};

const askUser = async () => {
    const userSurname = await question_name();
    const userName = await question_surname();
    console.log(`Hi, ${userName} ${userSurname}`);
    rdline.close();
};

askUser();

about 4 years ago · Juan Pablo Isaza Report

0

There is a function called resolve in js. I suggest you do function questionname() {...} instead of calling an arrow function. Also, use another name instead of resolve.

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!