I done some researching the other day for a project I've been working on and it brought me to this. This will work perfectly for what i'm wanting but I can't figure out how to get the collected answer in let's say the "question1" function and be able to use that data in the "main" function. Could someone offer me so advice?
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
const question1 = () => {
return new Promise((resolve, reject) => {
rl.question('q1 What do you think of Node.js? ', (answer) => {
resolve()
})
})
}
const question2 = () => {
return new Promise((resolve, reject) => {
rl.question('q2 What do you think of Node.js? ', (answer) => {
resolve()
})
})
}
const main = async () => {
await question1()
await question2()
rl.close()
}
main()
As @Nick mentioned in the comment pass answer
into resolve()
method provided by Promise like this
const question = () => {
return new Promise((resolve, reject) => {
rl.question("q2 What do you think of Node.js? ", (answer) => {
resolve(answer);
});
});
};
const main = async () => {
const answer = await question();
};
Now you should be able to access answer in the main