Last active
July 6, 2017 00:53
-
-
Save fasiha/8ee923fd1a27d596af1d287785465231 to your computer and use it in GitHub Desktop.
Interactive command-line apps in Node.js—a quick taste
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
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