Skip to content

Instantly share code, notes, and snippets.

@latentflip
Last active August 29, 2015 14:03
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 latentflip/5020fa596a8cce897c2c to your computer and use it in GitHub Desktop.
Save latentflip/5020fa596a8cce897c2c to your computer and use it in GitHub Desktop.
This program asks the user to enter things, until they enter an empty string, when it finishes, printing the things they entered.
//This just sets up readline
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function getThingsFromTheCommandLine(done) {
//rl.question asks for a prompt, and calls callback with the result
rl.question("Enter something: ", function (answer) {
if (answer.trim() === '') return done([]);
getThingsFromTheCommandLine(function (r) {
done([answer].concat(r));
});
});
}
getThingsFromTheCommandLine(function (results) {
console.log('Entered: ', results);
rl.close();
});
// $ node experiment.js
// Enter something: 1
// Enter something: 2
// Enter something: 3
// Enter something: 4
// Enter something:
// Entered: [ '1', '2', '3', '4' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment