Last active
September 7, 2016 12:28
-
-
Save cambiata/0a1bd3bded4ef18a2cda to your computer and use it in GitHub Desktop.
tink.core.Future.trigger() example
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
import neko.Lib; | |
import sys.FileSystem; | |
import sys.io.File; | |
import tink.core.Future; | |
import tink.core.Outcome; | |
class FutureTriggerExample | |
{ | |
static function main() | |
{ | |
TxtLoaders.loadAll(['existing.txt', 'non-existing.txt']).handle(function(outcome) trace(outcome)); | |
} | |
} | |
class TxtLoader { | |
static public function load(url:String):Surprise<String, String> { | |
var f = Future.trigger(); | |
if (FileSystem.exists(url)) | |
f.trigger(Success( File.getContent(url))) | |
else | |
f.trigger(Failure('Can\'t find $url')); | |
return f.asFuture(); | |
} | |
} | |
class TxtLoaders { | |
static public function loadAll(urls:Array<String>): Future<Array<Outcome<String, String>>> { | |
return [ for (url in urls) TxtLoader.load(url) ]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment