-
-
Save image72/e7b68ac28f31158599e7b751ea26b91b to your computer and use it in GitHub Desktop.
Node.js - Read line from stdin asynchronously (async / await)
This file contains 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(); |
Author
image72
commented
Jul 28, 2023
const start = async () =>{
for await (const line of rl) {
console.log(line)
}
}
start()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment