Last active
August 19, 2024 13:25
-
-
Save mauroao/6511c31170e34b8761d71a091ceebe7d to your computer and use it in GitHub Desktop.
Node.js - Read line from stdin asynchronously (async / await)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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