Skip to content

Instantly share code, notes, and snippets.

@leontastic
Last active February 27, 2019 04:48
Show Gist options
  • Save leontastic/7f768bca98a0b5fc39ab0aac4a5d1a13 to your computer and use it in GitHub Desktop.
Save leontastic/7f768bca98a0b5fc39ab0aac4a5d1a13 to your computer and use it in GitHub Desktop.
// ask your user questions on the command line in sequence
import readline from "readline";
let answer: Promise<string>;
const askQuestion = (question: string) =>
new Promise<string>(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`${question}`, answer => {
rl.close();
resolve(answer);
});
});
const prompt = (question: string) => {
if (answer) answer = answer.then(() => askQuestion(question));
else answer = askQuestion(question);
return answer;
};
export default prompt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment