Skip to content

Instantly share code, notes, and snippets.

@lejonmanen
Last active October 29, 2023 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lejonmanen/b5c9c033a8fd30b9f30884dfe97012c8 to your computer and use it in GitHub Desktop.
Save lejonmanen/b5c9c033a8fd30b9f30884dfe97012c8 to your computer and use it in GitHub Desktop.
Using Node.js async readline
import { getQuestion } from './readline.js'
const [question, close] = getQuestion()
let input = await question('Please input a number')
console.log(`The number is: ${input}.`)
close()
import readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process'
function getQuestion() {
const rl = readline.createInterface({ input, output });
return [rl.question.bind(rl), rl.close.bind(rl)]
// Use like this:
// const [question, close] = getQuestion()
}
const [question, close] = getQuestion()
let answer = await question('What do you think of Node.js? ');
// You can use question multiple times. But don't forget to close when you're done.
console.log(`Thank you for your valuable feedback: ${answer}`);
close()
import readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process'
function getQuestion() {
const rl = readline.createInterface({ input, output });
return [rl.question.bind(rl), rl.close.bind(rl)]
// Use like this:
// const [question, close] = getQuestion()
}
export { getQuestion }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment