Created
July 3, 2012 02:33
-
-
Save adam-singer/3037164 to your computer and use it in GitHub Desktop.
Sample async split of control structure for futures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://goo.gl/xDA15 | |
I don't like nesting either, but I think trying to hide your flow control by writing a branching set of logic as a 'pipeline' is a mistake. | |
I would refactor into methods, like this (again, guessing at what you're trying to do): | |
main() => getEntries("wibble.txt"); | |
/// Reads the files listed in [filename] and returns their contents as strings | |
/// or null if [filename] doesn't exist | |
Future<List<String>> getEntries(String filename) { | |
final file = new File(filename); | |
return file.exists().chain((e) => e | |
? new Future.immediate(null) | |
: file.readAsText().transform(splitLine).chain(readFiles)); | |
} | |
Future<List<String>> readFiles(List<String> files) => | |
Futures.wait(files.map((f) => new File(f).readAsText()); | |
List<String> splitLines(String text) => text.split("\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment