Reading Files Asynchronously Using Dart
Dart is growing on me, and I really enjoy working with it. It has made web-programming fun again. But Dart isn’t all about the web, it can also do a whole bunch of other things, and just now I stumbled on a neat method to read multiple files asynchronously using Dart Futures. Check this out:
main() {
List futures;
List texts;
new Directory(path).list().listen((File f) {
var c = new Completer();
futures.add(c.future);
f.readAsString().then((content) {
texts.add(text);
c.complete(null);
}
})
Future.wait(futures).then(() {
// all files read now
});
}
That is, IMHO, very neat.
If you haven’t already tried Dart, then be sure to give it a whirl. Despite its young age (and alfa/beta status) it’s a very nice language and it holds great promise for those of us who utterly despise the horror that is Javascript.
If you’re @ Google+ then why not join the Dart Community?