title: Using loops with Futures in Dart
body: <p>Okay, so I have a List of Files and I need to run a function on each member of the list. I essentially want to do something like this:</p>
  
  <pre class="lang-dart prettyprint-override"><code>for(File file in files) {
      functionThatReturnsAFuture(file);
  }
  </code></pre>
  
  <p>But obviously this won't work, since the function that returns a Future fires of asynchronously. Is my only option something like this?</p>
  
  <pre class="lang-dart prettyprint-override"><code>List&lt;File&gt; files = new List&lt;File&gt;();
  // Add files somewhere
  
  Future processFile(int i) {
     return new Future.sync(() {
         //Do stuff to the file
         if(files.length&gt;i+1) {
             return processFile(i+1);
         }
     });
  }
  
  processFile(0);
  </code></pre>
  
  <p>EDIT:
  I suppose more context is important. The end goal is to combine several files into a single JSON object for submitting to a server. The FileReader object uses events to perform a read, so I used a Completer to create a wrapper function that provides a Future. I could let all of these run asynchronously, and then fire off an event that submits it when they are all done, but that's comparatively a lot of setup vs. a for-each-loop that makes it all work (if one exists, anyway). The core issue is needing to run a Future-returning function on a List of files and then perform an action that depends on them all having completed.</p>