title: Trying to get function to finish so it can return data in node.js with promises
body: <p>I am new to promises in javascript. I am using node.js in my bot. I have a function that makes a request to an API to get information on stocks and uses promises for the request.  I am trying to figure out how to make it so my application will read in all the data before it continues. I have no problem receiving data from API that all works. At the moment when I run the code application continues before function finishes gathering/returning data.</p>
  
  <p>The function that gets information from API with my attempt at promises:</p>
  
  <pre><code>day: function(member, stocks){
      var real = [];
  
      return new Promise(function(resolve, reject){  
      stockdata.realtime({
          symbols: stocks,
          API_TOKEN: world_token
      })
      .then(response =&gt; {
          //console.log(response);
          var i =0;
          received = response.symbols_returned;
          response.data.forEach(element =&gt; {
              real[i]= element;
              ++i;
          });
          if(received == 0){
          console.log("received zero stocks");
          return;
          }
      })
      .catch(error =&gt; {
          console.log(member, " had error getting realtime ", Date());
          return;
      });
  
      resolve(real);
  
      })
  
  }
  </code></pre>
  
  <p>Inside index.js where main code is:</p>
  
  <pre><code> var data = [];
   var myPromise = stockData.day(member, 'AAPL');
      myPromise.then(function(result){
          data = result;
          console.log("worked: ", data);
      }, function(err){
          console.log(err);
      })
  </code></pre>
  
  <p>I don't know if I am doing it completely wrong or if I am missing just one step but at the moment this is what my console says when I run the code. </p>
  
  <pre><code>ready
  worked:  []
  popsnipes101  Requested: AAPL  || received: 1 Mon Dec 09 2019 23:03:25 GMT-0700 (Mountain Standard 
  Time)
  </code></pre>