1

I wrote a little code using Promise:

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

let quest = "What is your name?";
let name = new Promise((resolve, reject) => {
    readline.question(`${quest} `, result => {
        readline.close();
        resolve(result);
    });
});

name.then(data => { 
    return console.log("Hi - " + data);
});

I would like to do the same with async await, but nothing comes out yet

thank you in advance.

function prompt (question) {
    let name;
    readline.question(`${question} `, (result) => {
        readline.close();
        name = result;  
    });
    return name;
}

(async function () {
    let  a = await prompt(quest);
    console.log(a);
}())
  • Your `prompt()` function is not marked `async` and does not return a Promise. – Pointy Apr 01 '23 at 17:16
  • If you want to go for a modern approach, use https://nodejs.org/api/readline.html#rlquestionquery-options which relieves you of having to create the `new Promise` yourselves – Bergi Apr 01 '23 at 17:24

0 Answers0