Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created October 15, 2018 10:16
Show Gist options
  • Save salmanx/40b31cc938d6706f6bf4b4fa7ae3825d to your computer and use it in GitHub Desktop.
Save salmanx/40b31cc938d6706f6bf4b4fa7ae3825d to your computer and use it in GitHub Desktop.
code example for readline module in node js
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
const realPerson = {
name: "",
sayings: []
};
rl.question("Who is real person? ", function(answer){
realPerson.name = answer;
rl.setPrompt(`What would ${realPerson.name} say? `);
rl.prompt();
rl.on('line', function(sayings){
realPerson.sayings.push(sayings.toLowerCase().trim());
if (sayings.toLowerCase().trim() === "exit") {
rl.close();
} else {
rl.setPrompt(`what else ${realPerson.name} would say? (type "exit" to leave)`)
rl.prompt();
}
});
});
rl.on('close', function(){
console.log("%s is a real person who is sayings %j", realPerson.name, realPerson.sayings);
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment