Skip to content

Instantly share code, notes, and snippets.

@labe-me
Last active December 26, 2015 00:39
Show Gist options
  • Save labe-me/7066351 to your computer and use it in GitHub Desktop.
Save labe-me/7066351 to your computer and use it in GitHub Desktop.
Testing promhx and PromiseM.dO haxe -lib promhx -lib monax -lib nodejs -main T -js test.js
import promhx.*;
import js.Node;
class User {
public var id : Int;
public var name : String;
public function new(id, name){
this.id = id;
this.name = name;
}
public function listMessages() : Promise<Array<Int>> {
return switch (id){
case 0: T.delayPromiseResult(null, null);
case 1: T.delayPromiseResult(null, [1,2,3]);
default: T.delayPromiseResult(null, [1,2,3]);
}
}
}
class T {
public static function delayPromiseResult<T>(err, val:T){
var p = new Promise();
Node.process.nextTick(function(){
if (err != null)
p.reject(err);
else
p.resolve(val);
});
return p;
}
static function getUser(id) : Promise<User> {
return switch (id){
case 0: delayPromiseResult(null, null);
case 1: delayPromiseResult(null, new User(id, "My User 1"));
default: delayPromiseResult("AN ERROR FOR "+id, null);
};
}
public static function test(userId){
PromiseM.dO({
user <= getUser(userId);
isValidUser <= ret(if (user == null) throw "USER NOT FOUND" else true);
msgs <= user.listMessages();
ret({ user:user, messages:msgs });
})
.then(function(res){
trace('RES: ${res}');
})
.error(function(err){
trace('ERR: ${err}');
});
}
public static function main(){
test(0);
test(1);
test(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment