title: How can I correctly use a Promise on an async function in node.js?
body: <p>I have an asynchronous function in node that reads a text file, puts the whole thing into a string, splits the string at every new line, puts them into an array and returns one at random. Here I have implemented a new Promise function to deal with it:</p>
  
  <pre><code>exports.readTextFileAndReturnRandomLine = function readTextFile(file)
  {
      //reads text file as string, splits on new line and inserts into array, returns random array element
      return new Promise((resolve, reject) =&gt;
      {
          var fs = require('fs');
          var textFile = fs.readFile(file, 'utf8', (err, data) =&gt;
          {
              if (err)
              {
                  return reject(err);
              }
              else
              {
                  var array = data.toString().split("\n");
                  var response = array[Math.floor(Math.random() * array.length)];
                  return resolve(response);
              }
          });
      });
  }
  </code></pre>
  
  <p>Here is the text file that the function is reading from:</p>
  
  <pre><code>Hello there, 
  Howdy, 
  I will remember your name, 
  Thanks for telling me, 
  Hi 
  Noted, 
  Thanks 
  Well hello there 
  Nice to meet you 
  The pleasure is all mine, 
  Nice name,
  </code></pre>
  
  <p>Now in my root node (app.js), I call the function like so:</p>
  
  <pre><code>intents.matches('RememberName', [
      function (session, args, next) {
          var nameEntity = builder.EntityRecognizer.findEntity(args.entities, 'name');
          if (!nameEntity)
          {
              builder.Prompts.text(session, "Sorry, didn't catch your name. What is it?");
          } else 
          {
              next({ response: nameEntity.entity });
          }
      },
      function (session, results) {
          if (results.response)
          {         
              fileReader.readTextFileAndReturnRandomLine('./text/remembername.txt').then(function(value) {
                  console.log(value + ", " + results.response);
              }).catch(function(reason) {
                  console.log(reason);
              });
          }
          else
          {
              session.send("Ok");
          }
      }
  ]);
  </code></pre>
  
  <p>The problem is that the <code>value</code> and <code>name</code> variables are not being printed out to the console in the order that I have put them in. Here is my <strong>actual</strong> output:</p>
  
  <pre><code>my name is chris
  , Chrisfor telling me,
  my name is Chris
  , Chris
  my name is Chris
  , Chris
  my name is Chris
  , Chrishere,
  my name is Chris
  , Chrisfor telling me,
  my name is Chris
  , Chrisasure is all mine,
  my name is Chris
  , Chris
  my name is Chris
  , Chris
  my name is Chris
  , Chrisllo there
  </code></pre>
  
  <p>And here is my <strong>expected</strong> output:</p>
  
  <pre><code>my name is Chris
  Hello there, Chris
  my name is Chris
  Howdy, Chris
  my name is Chris
  Nice to meet you Chris
  my name is Chris
  Nice name, Chris
  </code></pre>
  
  <p>I believe it has something to do with the synchronicity of it all but I can't for the life of me figure out what it is.</p>