An Await implementation inspired by CSharp's await keyword discovered in the async support in nodejs article.
haxelib git wait https://github.com/skial/wait.git
And add -lib wait
to your hxml
file.
You have two options, use Wait with Klas or not.
package ;
class Main implements Klas {
public function new() {
@:wait asyncTask(100, 10000, [success], [error]);
trace( success );
}
public function asyncTask(start:Int, finish:Int, success:String->Void, error:String->Void) {
// Do something
}
}
package ;
@:autoBuild( uhx.macro.Wait.build() )
class Main {
public function new() {
@:wait asyncTask(100, 10000, [success], [error]);
trace( success );
}
public function asyncTask(start:Int, finish:Int, success:String->Void, error:String->Void) {
// Do something
}
}
Wait transforms any method thats contains @:wait
by taking all code after the
encountered @:wait
. It then inserts a local method whose body contains
the captured code, before the encountered @:wait
.
- Both
[success]
and[error]
indicate to the build macro that these parameters are methods, both taking a single parameter themselves. - The code
@:wait callback(1, 2, [c, d, e])
tells the build macro to create a method that has three parameters namedc
,d
ande
. - The marker
[]
can appear at any point in a method call@:wait callback(1, [a, b], 2, 3, [c], 4, [d, e])
- An empty marker,
[]
equalsVoid->Void
.
The following example is the transformed method body of the constructor from the examples above.
public function new() {
var block0 = function(?success:String, ?error:String) {
trace(success);
};
asyncTask(100, 10000, block0.bind(_), block0.bind(null, _));
}
You can find Waits tests in the uhu-spec library.