Skip to content

Instantly share code, notes, and snippets.

@fasiha
Last active July 6, 2017 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasiha/8ee923fd1a27d596af1d287785465231 to your computer and use it in GitHub Desktop.
Save fasiha/8ee923fd1a27d596af1d287785465231 to your computer and use it in GitHub Desktop.
Interactive command-line apps in Node.js—a quick taste
function prompt() {
return new Promise((resolve, reject) => {
var stdin = process.stdin,
stdout = process.stdout;
stdin.resume();
stdout.write("> ");
stdin.once('data', function(data) {
resolve(data.toString().trim());
stdin.pause();
});
});
}
// Promisified from https://stackoverflow.com/a/32276364/500207
console.log("What say you???");
prompt().then(data => {
console.log(`Really? ‘${data}’?`);
console.log("Care to try again?");
prompt().then(data => console.log(`I give up… ‘${data.replace(/\s/g, '')}’ indeed!`));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment