Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using loops with Futures in Dart

Tags:

dart

future

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:

for(File file in files) {
    functionThatReturnsAFuture(file);
}

But obviously this won't work, since the function that returns a Future fires of asynchronously. Is my only option something like this?

List<File> files = new List<File>();
// Add files somewhere

Future processFile(int i) {
   return new Future.sync(() {
       //Do stuff to the file
       if(files.length>i+1) {
           return processFile(i+1);
       }
   });
}

processFile(0);

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.

like image 556
SanMadJack Avatar asked Jun 26 '14 18:06

SanMadJack


2 Answers

When you need to wait for multiple Futures to complete and you don't care about the order, you can use Future.wait():

Future.wait(files.map(functionThatReturnsAFuture))
  .then((List response) => print('All files processed'));

If order is important you can use Future.forEach() instead which waits for each Future to be completed before moving to the next element:

Future.forEach(files, functionThatReturnsAFuture)
  .then((response) => print('All files processed'));
like image 184
Pixel Elephant Avatar answered Oct 16 '22 05:10

Pixel Elephant


Dart supports async/await since quite some time which allows it to write as

someFunc() async {
  for(File file in files) {
    await functionThatReturnsAFuture(file);
  }
}
like image 23
Günter Zöchbauer Avatar answered Oct 16 '22 07:10

Günter Zöchbauer