Wait for user input from readline module (Node.Js)












0















I am creating a module to get experience and shorten some code. I have a piece of code which uses readline in a simplified manner, like var x = arkin.question("How old are you? ");. Readline doesn't wait for the answer. It produces this:




How old are you? undefined




Code:



const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

exports.question = function(q){

var response;

rl.setPrompt(q);
rl.prompt();
rl.on('line', (userInput) => {
response = userInput;
rl.close();
});

rl.on('close', () => {
return response;
});
}


I call it like this:



var age = arkin.question("How old are you? ");
console.log(age);


I have tried using this code:



rl.question(q, (userInput) => {
rl.close;
response = userInput;
return response;
});


Yet I get the same result. Thanks in advance for your help.










share|improve this question



























    0















    I am creating a module to get experience and shorten some code. I have a piece of code which uses readline in a simplified manner, like var x = arkin.question("How old are you? ");. Readline doesn't wait for the answer. It produces this:




    How old are you? undefined




    Code:



    const readline = require('readline');

    const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
    });

    exports.question = function(q){

    var response;

    rl.setPrompt(q);
    rl.prompt();
    rl.on('line', (userInput) => {
    response = userInput;
    rl.close();
    });

    rl.on('close', () => {
    return response;
    });
    }


    I call it like this:



    var age = arkin.question("How old are you? ");
    console.log(age);


    I have tried using this code:



    rl.question(q, (userInput) => {
    rl.close;
    response = userInput;
    return response;
    });


    Yet I get the same result. Thanks in advance for your help.










    share|improve this question

























      0












      0








      0








      I am creating a module to get experience and shorten some code. I have a piece of code which uses readline in a simplified manner, like var x = arkin.question("How old are you? ");. Readline doesn't wait for the answer. It produces this:




      How old are you? undefined




      Code:



      const readline = require('readline');

      const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
      });

      exports.question = function(q){

      var response;

      rl.setPrompt(q);
      rl.prompt();
      rl.on('line', (userInput) => {
      response = userInput;
      rl.close();
      });

      rl.on('close', () => {
      return response;
      });
      }


      I call it like this:



      var age = arkin.question("How old are you? ");
      console.log(age);


      I have tried using this code:



      rl.question(q, (userInput) => {
      rl.close;
      response = userInput;
      return response;
      });


      Yet I get the same result. Thanks in advance for your help.










      share|improve this question














      I am creating a module to get experience and shorten some code. I have a piece of code which uses readline in a simplified manner, like var x = arkin.question("How old are you? ");. Readline doesn't wait for the answer. It produces this:




      How old are you? undefined




      Code:



      const readline = require('readline');

      const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
      });

      exports.question = function(q){

      var response;

      rl.setPrompt(q);
      rl.prompt();
      rl.on('line', (userInput) => {
      response = userInput;
      rl.close();
      });

      rl.on('close', () => {
      return response;
      });
      }


      I call it like this:



      var age = arkin.question("How old are you? ");
      console.log(age);


      I have tried using this code:



      rl.question(q, (userInput) => {
      rl.close;
      response = userInput;
      return response;
      });


      Yet I get the same result. Thanks in advance for your help.







      javascript node.js node-modules readline






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 20:17









      Arkin SolomonArkin Solomon

      808




      808
























          1 Answer
          1






          active

          oldest

          votes


















          1














          whenever you call arki.question it registeres the event listeners .on("line") and .on("close") then returns from the function. Whatever you returning from .on("close") event listener question function does not know about it, because it is no longer on the call stack. you can either use a callback or promises with async...await to get your result.



          with callbacks



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q , cb ){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          return cb(response);
          });
          };


          you call it like this



          var age  = arki.question("how old are you? ", resp => {
          console.log(resp);
          });


          with promises



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          return new Promise(( resolve , reject) => {

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          resolve(response);
          });

          });


          };


          you call it like this



          arki.question("how old are you? ").then( response => console.log(response) );


          or



          ; ( async () => {
          console.log(await arki.question("how old are you? "));
          })();





          share|improve this answer
























          • Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

            – Arkin Solomon
            Dec 30 '18 at 21:56











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981103%2fwait-for-user-input-from-readline-module-node-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          whenever you call arki.question it registeres the event listeners .on("line") and .on("close") then returns from the function. Whatever you returning from .on("close") event listener question function does not know about it, because it is no longer on the call stack. you can either use a callback or promises with async...await to get your result.



          with callbacks



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q , cb ){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          return cb(response);
          });
          };


          you call it like this



          var age  = arki.question("how old are you? ", resp => {
          console.log(resp);
          });


          with promises



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          return new Promise(( resolve , reject) => {

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          resolve(response);
          });

          });


          };


          you call it like this



          arki.question("how old are you? ").then( response => console.log(response) );


          or



          ; ( async () => {
          console.log(await arki.question("how old are you? "));
          })();





          share|improve this answer
























          • Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

            – Arkin Solomon
            Dec 30 '18 at 21:56
















          1














          whenever you call arki.question it registeres the event listeners .on("line") and .on("close") then returns from the function. Whatever you returning from .on("close") event listener question function does not know about it, because it is no longer on the call stack. you can either use a callback or promises with async...await to get your result.



          with callbacks



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q , cb ){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          return cb(response);
          });
          };


          you call it like this



          var age  = arki.question("how old are you? ", resp => {
          console.log(resp);
          });


          with promises



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          return new Promise(( resolve , reject) => {

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          resolve(response);
          });

          });


          };


          you call it like this



          arki.question("how old are you? ").then( response => console.log(response) );


          or



          ; ( async () => {
          console.log(await arki.question("how old are you? "));
          })();





          share|improve this answer
























          • Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

            – Arkin Solomon
            Dec 30 '18 at 21:56














          1












          1








          1







          whenever you call arki.question it registeres the event listeners .on("line") and .on("close") then returns from the function. Whatever you returning from .on("close") event listener question function does not know about it, because it is no longer on the call stack. you can either use a callback or promises with async...await to get your result.



          with callbacks



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q , cb ){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          return cb(response);
          });
          };


          you call it like this



          var age  = arki.question("how old are you? ", resp => {
          console.log(resp);
          });


          with promises



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          return new Promise(( resolve , reject) => {

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          resolve(response);
          });

          });


          };


          you call it like this



          arki.question("how old are you? ").then( response => console.log(response) );


          or



          ; ( async () => {
          console.log(await arki.question("how old are you? "));
          })();





          share|improve this answer













          whenever you call arki.question it registeres the event listeners .on("line") and .on("close") then returns from the function. Whatever you returning from .on("close") event listener question function does not know about it, because it is no longer on the call stack. you can either use a callback or promises with async...await to get your result.



          with callbacks



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q , cb ){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          return cb(response);
          });
          };


          you call it like this



          var age  = arki.question("how old are you? ", resp => {
          console.log(resp);
          });


          with promises



          const readline = require('readline');

          const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
          });

          exports.question = function(q){

          var response;

          rl.setPrompt(q);
          rl.prompt();

          return new Promise(( resolve , reject) => {

          rl.on('line', (userInput) => {
          response = userInput;
          rl.close();
          });

          rl.on('close', () => {
          resolve(response);
          });

          });


          };


          you call it like this



          arki.question("how old are you? ").then( response => console.log(response) );


          or



          ; ( async () => {
          console.log(await arki.question("how old are you? "));
          })();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 30 '18 at 20:37









          0.sh0.sh

          1,5701125




          1,5701125













          • Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

            – Arkin Solomon
            Dec 30 '18 at 21:56



















          • Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

            – Arkin Solomon
            Dec 30 '18 at 21:56

















          Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

          – Arkin Solomon
          Dec 30 '18 at 21:56





          Ok, now i understand, but doing this is just as complex as using it normally. Thanks for your help!

          – Arkin Solomon
          Dec 30 '18 at 21:56


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981103%2fwait-for-user-input-from-readline-module-node-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'