title: Read Multiple Files With JavaScript and Wait for Result
body: <p>I would like to start by saying that (coming from c++ and python) I am totally new to JS and so I welcome any wise suggestions regarding my code. </p>
  
  <p>I wish to read a number of files using the HTML5 file API, then open them with JS, perform some manipulation and download the results zipped. My problem is that reading the files seems to be an asynchronous operation, and I don't really see an elegant way to wait for them all to finish and then zip the results. </p>
  
  <p>One possible solution is presented here:
  <a href="https://stackoverflow.com/a/17491515">https://stackoverflow.com/a/17491515</a>
  but I am wondering if one can do better than using a global flag.</p>
  
  <p>I also have a problem with retrieving the result from the async function as I have no idea how to get back <code>new_file_list</code> in <code>changeCharsInFiles</code>.</p>
  
  <p>Thank you!</p>
  
  <p>Code example:
  HTML:</p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
      &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"&gt;
      &lt;/head&gt;
      &lt;body &gt;
          &lt;div class="container"&gt;
              &lt;div class="jumbotron"&gt;
                  &lt;h3&gt;Add Files Here&lt;/h3&gt;
                  &lt;input type="file" id="the-file-field" multiple&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"&gt;&lt;/script&gt;
          &lt;script src="http://cdn.jsdelivr.net/g/filesaver.js"&gt;&lt;/script&gt;
          &lt;script&gt;
           ########### SEE JS BELOW #####################
      &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>JS:</p>
  
  <pre><code>if (window.File &amp;&amp; window.FileReader &amp;&amp; window.FileList &amp;&amp; window.Blob) {
  
                  //functions
                  function zipMyFilesAndSave(file_list){
  
                      var zip = new JSZip();
                      for (var i = 0; i&lt;file_list.length; i+=1)
                      {
                          zip.file(file_list[i].name, file_list[i]  );
                      }
                      zip.generateAsync({type:"blob"}).then(
                      function (blob) {                                      
                          saveAs(blob, "hello.zip");                         
                      }, 
                      function (err) {
                          jQuery("#blob").text(err);
                      }); 
                  }
  
                  function changeCharsInFiles(file_list){
                      var new_file_list = [];
  
                      for (var i = 0; i&lt;file_list.length; i+=1)
                      {
                          var file = file_list[i]
  
                          var reader = new FileReader();
                          reader.onload = function() {
                              //alert(reader.result);
                              var txt  = reader.result;
                              console.log("txt: ",txt)
  
                              var new_txt = ""
                              var allTextLines = txt.split(/\r\n|\n/);
  
                              for (var j = 0; j&lt;allTextLines.length; j+=1)
                              {
                                  var res = allTextLines[j].replace("a", "A");
                                  res = res.replace("b", "B");
                                  res = res.replace("c", "C");
                                  res = res.replace("d", "D");
  
                                  new_txt += res + "\n"
  
                              }
                              console.log("new_txt: ", new_txt)
                              var new_file = new Blob([new_txt], {type: "text/plain"});
  
                              new_file_list.push(new_file); //&lt;---------------------------how do I get this back?
                          }
                          reader.readAsText(file);    
  
                      }
  
  
                      return new_file_list;
                  }
  
                  //watcher
                  $( "#the-file-field" ).change(function() {
                      console.log("files have been chosen")
                      var file_list = this.files
                      file_list = changeCharsInFiles(file_list)
                      zipMyFilesAndSave(file_list)    
                  });
  
  
              } else {
                  alert('The File APIs are not fully supported in this browser.');
              }
  </code></pre>