title: Can't get the result of an asynchronous function using Promise
body: <p>I'm writing a node.js program to read from console. Therefore ,I used "readline" module, but even If I called promise to get Its result in order to manipulate it (in both ways), but still I don't get the expected result:</p>
  
  <blockquote>
    <p>The below code is the module I want to use:</p>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>module.exports.getContentFromPrompt=function (query){
      var read= require('readline');
      var input;
      var rl=read.createInterface({
          input:process.stdin,
          output: process.stdout
      }); 
      rl.question(query,(answer)=&gt;{
          input=answer;
          console.log(answer);
      });
      var promise = new Promise((resolve, reject)=&gt;{
          resolve(input);
      });
      promise.then((successMessage)=&gt;{
          console.log(successMessage);
      })
      rl.close();
      return input;
  };</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <p>The following snippet is the code when I call my module</p>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>console.log("Starting app.js");
  const fs= require('fs');
  const _=require('lodash');
  //const prompt= require('prompt')
  var readline= require('readline');
  var process= require('process');
  
  // const notes= require('./notes.js')
  var inputFromPrompt = require('./read.js');
  arg=process.argv;
  var notes=new Map();
  if(arg[2]=="list-all"){
      notes.array.forEach((value, key) =&gt; {
          console.log(`the value of the key: ${key} is ${value}`);
      });
  }
  else if(arg[2]=="search"){
      var elt2search=inputFromPrompt.getContentFromPrompt("what is the key of the element you're searching:");
      searchedNote=notes.get(elt2search);
      if(searchedNote==undefined){
          console.log('the element with the key '+ elt2search+' does not exist');
      }else{
          console.log('the value of the element with the key '+elt2search+' is: '+searchedNote); 
      }
  
  }
  else if(arg[2]=="remove"){
      var elt2delete=inputFromPrompt.getContentFromPrompt("what is the key of the element you want to delete:");
      searchedNote=notes.delete(elt2delete);
      if(searchedNote==undefined){
          console.log('the element with the key '+ elt2delete+' does not exist');
      }else{
          console.log('the value of the element withe the key '+elt2delete+' is: '+elt2delete)
      }
  }
  else if(arg[2]=="add"){
      // var promise4Key = new Promise(function(resolve, reject){
      //     resolve(inputFromPrompt.getContentFromPrompt("what is the key of the element you want to add:"));
      // })
      // var promise4Value = new Promise((resolve, reject)=&gt;{
      //     resolve(inputFromPrompt.getContentFromPrompt("what is the value of the element you want to add:"));
      // })
      var key, value;
      // promise4Key.then((successMessage)=&gt;{
      //     key=successMessage;
      // })
      // promise4Value.then((successMessage)=&gt;{
      //     value=successMessage;
      // });
      key= inputFromPrompt.getContentFromPrompt("what is the key: ");
      value= inputFromPrompt.getContentFromPrompt("what is the value: ");
      notes=notes.set(key,value);
      notes.forEach((value, key)=&gt;{
          console.log("The value is: "+ value+" of the key "+key);
      });
  }
  else{
      console.log('The command you typed: "'+ process.argv[2]+'" is not valid');
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>In the above code, the commented line shows the alternative to use the Promise class.
  The following lines shows the result that is prompted:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>C:\node\notes&gt;node app.js add
  Starting app.js
  what is the value: The value is: undefined of the key undefined
  undefined
  undefined</code></pre>
  </div>
  </div>
  
  Can you tell me, what is the problem and how to correct that issue.</p>