title: Comparaison between different input files in javascript, cannot reach global variable
body: <p>I'm trying to open different JSON files and compare each others values.</p>
  
  <p>To do it, I used <a href="https://stackoverflow.com/a/13975217/13433513">this</a> to read the files. But I'm trying to save every data in a global variable 'data'. I think it's a asynchronous mistake but I'm pretty new to javascript and I didn't understand where the error come from.</p>
  
  <p>Here is my code :</p>
  
  <pre><code>var data = {}
  function readmultifiles(files) {
      var reader = new FileReader();
  
      function readFile(index) {
          if( index &gt;= files.length ) return;
          var file = files[index];
          reader.onload = function(e) {  
              // get file content  
              var bin = e.target.result;
              bin = JSON.parse(bin);
              for(task in bin['values']){
                  addData(bin['info']['date'],task,bin['values'][task]);
              }
              // do sth with bin
              readFile(index+1);
          }
          reader.readAsBinaryString(file);
  
      }
  
      readFile(0);
      console.log("readmultifiles");
      console.log(data);
      return data;
  }
  
  function addData(date, task, value){
      if(data[task] == undefined){
          data[task] = {};
      }
      data[task][date] = value;
  }
  
  var fileInput = document.querySelector('#file');
  fileInput.addEventListener('change', function() {
      console.log(fileInput);
  
      var files = fileInput.files;
      readmultifiles(files);
      console.log("index");
      console.log(data);
      console.log(data['task_1']); // I can't display this object because 'undefinned'
  
  });
  </code></pre>
  
  <p>What happens? When I'm trying to watch 'data', firefox console display me the object but I cannot watch inside the object.</p>
  
  <p><a href="https://i.stack.imgur.com/PbrVX.png" rel="nofollow noreferrer">Firefox display</a></p>
  
  <p>What do I need to do to make a good solution. Should I use timers to wait?</p>