simple-question-answer-wo-packages.js 582 B

12345678910111213141516171819202122232425
  1. // source: https://stackoverflow.com/a/46488389
  2. //
  3. var question = function(q) {
  4. return new Promise( (res, rej) => {
  5. const stdin_proc = process.openStdin()
  6. process.stdout.write(q)
  7. function catchInput(text) {
  8. const ans = text.toString().trim()
  9. res(ans);
  10. stdin_proc.pause(); // stop reading
  11. }
  12. // We use once to avoid "MaxListenersExceededWarning" error
  13. stdin_proc.once('data', catchInput);
  14. });
  15. };
  16. //
  17. (async function main() {
  18. var answer;
  19. while ( answer != 'yes' ) {
  20. answer = await question('Are you sure? ');
  21. }
  22. console.log( 'finally you are sure!');
  23. })();