title: Why is fs.readdir returning `undefined`?
body: <p>The below code builds a dictionary (associative array) of the files in a folder, skipping directories and hidden (.*) files. The coe works because the <code>console.log(file_dict)</code> shows the dictionary before being returned. However, when I call the <code>get_files</code> function, the return value from <code>return file_dict</code> is  <code>undefined</code></p>
  
  <p>I'm pretty sure this problem is because <code>readdir</code> is async. So, I need to switch, to <code>readdirSync</code>. BUT conceptually I'm not getting why the returned value in the async version is undefined.</p>
  
  <p>What am I missing?</p>
  
  <p><strong>file-explorer.js</strong></p>
  
  <pre><code>    var fs = require('fs');
  
      var get_files = function() {
          fs.readdir(__dirname + '/content/', function (err, files) { 
            if (!files.length) {
              return console.log('    \033[31m No files to show\033[39m\n');
            }
  
            // called for each file walked in the directory
            var file_dict = {};
            var file_index = 0;
  
            function file(i) {
              var filename = files[i];
  
              fs.stat(__dirname + '/' + filename, function (err, stat) {
  
                if (stat.isDirectory() || filename[0] == '.'  ) {
                      // do nothing, skip these dictionaries and dot files
                } else {
                  ++file_index;
                  file_dict[file_index] = filename;
                };
  
                if (++i == files.length) {
                              console.log('right before returning');
                  console.log(file_dict);
                  return file_dict;
                } else {
                  // continue getting files
                  file(i);
                };
              });
            }
            file(0);
          });
      }
  
  
  console.log ( get_files() ); // returns undefined
  </code></pre>
  
  <p>Thanks.</p>