title: Node.js Q promise forEach returning undefined
body: <p>Using Q for Node.js, I am promising a HTTP request, and upon fulfillment calling another function passing in the response of that HTTP request, that function then iterates over a JSON array from the HTTP requests, builds up  a new array, and returns it.</p>
  
  <p>Debugging <code>Reddit.prototype.parseData</code> I can see the HTTP JSON is passed in, and within the for statement I can console.log <code>data</code> as it's been built, but at the end of the foreach I cannot console.log, or return the data object, it returns undefined</p>
  
  <p>Reddit.js</p>
  
  <pre><code>var Reddit = function(){
      this.endpoint = "https://www.reddit.com/r/programming/hot.json?limit=10";
  }
  
  Reddit.prototype.parseData = function(json, q){
      var dataLength  = json.data.children.length,
          data        = [];
  
      for(var i = 0; i &lt;= dataLength; i++){
          var post    = {};
  
          post.url    = json.data.children[i].data.url;
          post.title  = json.data.children[i].data.title;
          post.score  = json.data.children[i].data.score;
  
          console.log(data); //returns data    
  
          data.push(post);
      }
  
      console.log(data); // returns undefined
  
      return data;
  }
  
  module.exports = Reddit;
  </code></pre>
  
  <p>Feeds.js</p>
  
  <pre><code>var https   = require('https'),
          q       = require('q'),
          Reddit  = require('./sources/reddit');
  
  var Feeds = function(){
      this.reddit = new Reddit();
      console.log(this.parseRedditData()); //undefined
  }
  
  Feeds.prototype.getData = function(endpoint){
      var deferred = q.defer();
  
      https.get(endpoint, function(res) {
          var body = '';
  
          res.on('data', function(chunk) {
              body += chunk;
          });
  
          res.on('end', function() {
             deferred.resolve(JSON.parse(body));
          });
      }).on('error', function(e) {
          deferred.reject(e);
      });
  
      return deferred.promise;
  }
  
  Feeds.prototype.parseRedditData = function(){
      var _this      = this;
  
      this.getData(this.reddit.endpoint).then(function(data){
          return _this.reddit.parseData(data);
      });
  
  
  }
  
  var fe = new Feeds()
  </code></pre>