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()