Last active
August 29, 2015 14:03
-
-
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 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
//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