0

I was just messing around with the readline module but have thought of mapping over a list of questions and then collecting and logging the list of answers in an async await. In the code below, I'm getting What is your name? [ Promise { <pending> }, Promise { <pending> }, Promise { <pending> } ] then closes after entering something

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

let questionList = [
    "What is your name? ",
    "What is your major? ",
    "What is your favorite programming language? ",
]

function askQuestions() {
    function askQuestion(question: string) {
        return new Promise((resolve, reject) => {
            rl.question(question, (answer: string) => {
                rl.close()
                return resolve(answer)
            })
        })
    }
    try {
        let answers = questionList.map(async (question: string) => {
            return await askQuestion(question)
        })
        console.log(answers);
        
    } catch (error) {
        console.log(error)
    }
}

askQuestions()
Nugget
  • 81
  • 1
  • 6
  • 23
  • 1
    Does this answer your question? [How to wait for a promise to resolve inside a map?](https://stackoverflow.com/questions/59137483/how-to-wait-for-a-promise-to-resolve-inside-a-map) – eol May 11 '22 at 07:08
  • 1
    @eol Yep, that cleared things up. Thanks dude. Also I just found out, this question is a dupli of this https://stackoverflow.com/questions/57176074/series-of-user-inputs-in-javascript-using-readline-where-corresponding-questions – Nugget May 11 '22 at 07:58

0 Answers0