Skip to content

Instantly share code, notes, and snippets.

@mauroao
Last active November 13, 2023 14:59
Show Gist options
  • Save mauroao/6511c31170e34b8761d71a091ceebe7d to your computer and use it in GitHub Desktop.
Save mauroao/6511c31170e34b8761d71a091ceebe7d to your computer and use it in GitHub Desktop.
Node.js - Read line from stdin asynchronously (async / await)
const readline = require('readline');
const readLineAsync = () => {
const rl = readline.createInterface({
input: process.stdin
});
return new Promise((resolve) => {
rl.prompt();
rl.on('line', (line) => {
rl.close();
resolve(line);
});
});
};
const run = async () => {
console.log('what is your name ? ');
const line = await readLineAsync();
console.log(`Your name is ${line}`);
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment