Skip to content

Instantly share code, notes, and snippets.

@postite
Created April 11, 2013 12:20
Show Gist options
  • Save postite/5362938 to your computer and use it in GitHub Desktop.
Save postite/5362938 to your computer and use it in GitHub Desktop.
promhx test
import promhx.Promise;
using Main.HttpExtensions;
import haxe.Http;
using Main;
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
#end
class Main
{
function new()
{
new Http("/req/one").promise()
.pipe(function(t) return new Http("req/two").promise())
.pipe(function(t)return new Http("req/three").promise())
.then(function(t)trace("finished"));
pop("er")
.pipe(function(d) return pip(3))
.pipe(function(d) return pip(4+d))
.then(function(z)trace("sum="+z));
new Http("/req/un").promise()
.pipe(function(t)return pop("deux"))
.pipe(function(t)return pop("trois"+t))//is not fired
.then(function(z)trace("total="+z));
}
function pop(s):Promise<String>{
var p=new Promise<String>();
trace("pop ="+s);
p.resolve(s);
return p;
}
function pip(s:Int):Promise<Int>{
var p=new Promise<Int>();
trace("pip ="+s);
p.resolve(s);
return p;
}
static public function main()
{
var app = new Main();
}
}
import haxe.Http;
class HttpExtensions{
public static function promise(h:Http,request = false, ?err:Dynamic->Dynamic){
var p = new Promise<String>(err);
h.onError = p.reject;
var set = false;
h.onData = function(x){
if (!set) p.resolve(x);
else set = true;
}
h.request(request);
// trace("p="+p);
return p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment