Skip to content

Instantly share code, notes, and snippets.

@themarcba
Created July 23, 2021 14:56
Show Gist options
  • Save themarcba/801f7a6b9bcd3df9cc9558fe9917506c to your computer and use it in GitHub Desktop.
Save themarcba/801f7a6b9bcd3df9cc9558fe9917506c to your computer and use it in GitHub Desktop.
// A dependency-free, await-ready function for Node.js
// to get user input in the console
// Author: Marc Backes (@themarcba
const getInput = query => {
return new Promise((resolve, reject) => {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
})
readline.question(`${query} `, answer => {
readline.close()
resolve(answer)
})
})
}
const run = async () => {
const name = await getInput(`What is your name?`)
console.log(`Your name is ${name}`)
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment