title: Using q promises with map to get file contents
body: <p>I'm trying to use the <code>q</code> promise library to get the names of <code>.json</code> files in a directory and return the contents of those files. I'm able to get the filenames, but I'm having trouble using <code>arr.map</code> in conjunction with promises in order to read the <em>contents</em> of the file. I'd like <code>getContents()</code> to return a mapped array with the contents of the specified filenames.</p>
  
  <pre><code>function readDir() {
      var deferred = q.defer();
      fs.readdir('users', function(err, data) {
          if (err) console.log(err);
          deferred.resolve(data);
      })
      return deferred.promise;
  }
  
  function getContents(filenames) {
      return filenames.map(function(filename) {
          fs.readFile('users/' + filename, 'utf8', function(err, result) {
              return result;
          });
      });
  }
  
  readDir()
      .then(getContents)
      .then(function(fileContents) {
          // currently returns undefined
          console.log(fileContents);
      });
  </code></pre>
  
  <p>How can I properly wrap <code>getConents()</code> in a promise so that the contents of my array are not equal to undefined?</p>