Created
October 15, 2018 10:16
-
-
Save salmanx/40b31cc938d6706f6bf4b4fa7ae3825d to your computer and use it in GitHub Desktop.
code example for readline module in node js
This file contains hidden or 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
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