title: How to find out if reading dir is completed
body: <p>everyone
  I am working on some sort of image view application using node-webkit. I made a function to read dir inside the given location and search for the image files(*.jpg and *.png). Code I used is as follows:<br>
      <b>app.js</b></p>
  
  <pre><code>    var fs = require("fs");
      var gui = require('nw.gui');
      var win = gui.Window.get();
      var directory;
      var db = require('diskdb');
      var path = require('path')
      db = db.connect("lib/collections", ['temp']);
  
  
          function chooseFile(name) {
          var chooser = $(name);
          scr = 0;
          chooser.change(function(evt) {
              directory = $(this).val();
              var asdf;
              console.clear();
              readDir(directory);
              $(this).val('').hide();
          });
      }
  
  
      function readDir(directory){
      c = 0;
      console.log("reading "+directory);
  if(fs.statSync(directory).isDirectory() == true){
      fs.readdir(directory,function(err,files){
          if (err){
              console.log(err);
              return;
          }           
          var ext;
          files.forEach(function(file){
              console.log("Got what: "+file);
              var fulls = directory+"\\"+file;
              if(file.indexOf(".") != 0){
                  if(path.extname(fulls) == ""){
                      console.log("Got a directory: "+file);
                      if(fs.statSync(fulls).isDirectory() == true){
                          readDir(fulls);
                      }
                  }
                  else{
                      console.log("Got a file: "+file);
                      if(checkExtension(file, 'jpg,png')){
                          scr++;
                          c = saveTemp(fulls,scr,file);
                      }
                  }
              }
          });
          if(c == 1){
              loadgun();
          }
      });
  }
    }
  
  
          function loadgun(){
      if(db.temp.count()!=0){
          for(i=1;i&lt;=db.temp.count();i++){
              var asd = db.temp.findOne({'id':i});
              var theTempScript = $("#tmpl-imgholder").html();
              var theTemp = Handlebars.compile(theTempScript);
              $("#ContentWrapper").append(theTemp({"fulls":asd.file, "id":asd.id, "title":asd.title}));
          }
      }
  }
  
    saveTemp = function(file,id, title) {
      var savedUser = db.temp.save({
          file:file,
          id:id,
          title:title 
      });
      return 1;
  
  };
  
  
      function checkExtension(str, ext) {
          extArray = ext.split(',');
          for(i=0; i &lt; extArray.length; i++) {
              if(str.toLowerCase().split('.').pop() == extArray[i]) {
                  return true;
              }
          }
          return false;
      };
  
  
  $(document).ready(function(){
  if(db.temp.count() != 0){
     loadgun();
  }
  else{
      $('#blah').css('display','block');
      chooseFile('#blah');
  }
  });
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;html&gt;
  .
  .
  .
  &lt;body&gt;
  &lt;input type="file" nwdirectory id="blah" style="display:none"/&gt;
      &lt;script src="js/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/app.js"&gt;&lt;/script&gt;
      &lt;script src="js/handlebars.js"&gt;&lt;/script&gt;
      &lt;script id="tmpl-imgholder" type="x-handlebars-template"&gt;
          &lt;div class="image-container grid__item" data-id="{{id}}" data-src="{{fulls}}"&gt;
              &lt;div class="cover" style="background:url({{fulls}})"  title="{{title}}"&gt;&lt;/div&gt;
              &lt;div class="info"&gt;
                  &lt;div class="title"&gt;{{title}}&lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="clear"&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here i tried to load data using loadgun function. Its hangs the node webkit window. If I could know when the readDir function terminates then i could do load the required datas.
  If there is another way please do say.
  Thanks in advance.</p>