basro / hx-jsasync Goto Github PK
View Code? Open in Web Editor NEWHaxe lib for generating native JS async functions in an ergonomic and type safe way.
Haxe lib for generating native JS async functions in an ergonomic and type safe way.
Using .jsawait() as a static extension doesn't work for me - the compiler complains with
src/Main.hx:8: characters 24-31 : JSASync: js.lib.Promise<String> has no field jsawait
I guess it's because the promise is typed as js.lib.Promise.Thenable
, not as js.lib.Promise
(as in the deprecated .await() function, wich works!) When replacing
public static extern inline function jsawait<T>(promise:js.lib.Promise.Thenable<T>):T
with
public static extern inline function jsawait<T>(promise:js.lib.Promise<T>):T
it works as expected.
@:native meta not work for function in @:jsasync
extern class Some{
@:native('catch')
function catch_(d:Dynamic):Dynamic;
....
@:jsasync
public static function mainAsync(){
XXXX.catch_(XX);//will gen js code XXXX.catch_ instead of XXXX.catch
}
Hello, do you have any ideas how to handle for loops containing async functions with your nice library? It's common to parse an array of something that will use await inside the loop. I'm using my asynctools library currently, but switching to jsasync would be great...!
Hi @basro
I'm exploring your library and came across a problem of using nested async
functions. It seems like the macro only generates the first async
.
Consider the following haxe code:
import jsasync.JSAsyncTools.jsawait;
import jsasync.IJSAsync;
class Main implements IJSAsync {
static function main() {
do_thing();
}
@:jsasync static function do_thing() {
our_promise().then(@:jsasync function(v) {
var v2 = jsawait(our_promise());
trace(v2);
});
}
static function our_promise() {
return new js.lib.Promise(function(resolve, reject) {
resolve('promise completed');
});
}
}
When I initially ran into this problem I didn't even have the second @:jsasync
identifier on my anonymous function, but am including it here to show that it just gets removed in the JS output:
// Generated by Haxe 4.1.5
(function ($global) { "use strict";
var Main = function() { };
Main.main = function() {
Main.do_thing();
};
Main.do_thing = async function() {
// missing async identifier below
Main.our_promise().then(function(v) {
console.log("src/Main.hx:12:",(await Main.our_promise()));
});
};
Main.our_promise = function() {
return new Promise(function(resolve,reject) {
resolve("promise completed");
});
};
var haxe_iterators_ArrayIterator = function(array) {
this.current = 0;
this.array = array;
};
haxe_iterators_ArrayIterator.prototype = {
hasNext: function() {
return this.current < this.array.length;
}
,next: function() {
return this.array[this.current++];
}
};
var jsasync_JSAsyncTools = function() { };
var jsasync_impl_Helper = function() { };
Main.main();
})({});
And so we then receive the JS error:
console.log("src/Main.hx:12:",(await Main.our_promise()));
SyntaxError: Unexpected identifier
I'm still learning and doing my best to understand promises. I believe we can refactor the explicit promise out into another await
so this works:
var v = jsawait(our_promise());
var v2 = jsawait(our_promise2(v));
trace(v2);
/*
our_promise().then(@:jsasync function(v) {
var v2 = jsawait(our_promise());
trace(v2);
});
*/
I'm not sure if your library already looks for this and it's a bug or would be new functionality. I assumed incorrectly that any child functions within a parent async function are automatically marked async, but that's apparently not the case.
If it's new, would you please consider adding this :)
Thanks.
I receive an Uncaught exception Stack overflow
randomly. This 1000+ line error message appears in the Problems
tab within VScode:
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:109: characters 24-67 : Uncaught exception Stack overflow
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:1: character 1 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:147: characters 35-39 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:155: characters 34-38 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:147: characters 35-39 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:155: characters 34-38 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:305: characters 13-17 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:167: characters 29-54 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:169: characters 56-69 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:305: characters 13-17 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/ExprTools.hx:167: characters 29-54 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:126: characters 14-27 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:129: characters 10-19 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:163: characters 10-39 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:46: characters 16-44 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Helper.hx:27: characters 10-48 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:109: characters 24-67 : Called from here
C:\Program Files\Haxe Toolkit\haxe\std/haxe/macro/Context.hx:51: characters 10-36 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:72: characters 5-58 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:166: characters 15-37 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/impl/Macro.hx:31: characters 44-72 : Called from here
C:\Program Files\Haxe Toolkit\haxe\lib\jsasync/1,2,1/src/jsasync/JSAsync.hx:17: characters 10-50 : Called from here
...
Unfortunately the message just repeats... And my application compiles successfully. So far, any runtime errors don't appear to be associated with jsasync.
Not sure how helpful this info is to find the error, but hopefully this is a start.
I'm also using Haxe 4.1.5.
Thanks.
how to write haxe code eq
for await (const msg of source) {
// exact matching
node2.handle('/your-protocol', ({ stream }) => {
pipe(
stream,
async function (source) {
for await (const msg of source) {
console.log(msg.toString())
}
}
)
})
how to support AsyncGenerator?
node.handle('/your-protocol', (d) -> {
ItPipe.pipe(d.stream, JSAsync.jsasync(function(source:Array<Promise<Any>>) {
//don't know how to write here?
}));
});
A module-level function such as:
@:jsasync @:expose("run") function run() {}
Turns into:
function async Main_run() {}
Seems to work ok with a wrapper class.
class Main implements IJSAsync {
public static function timer(msec:Int) {
return new Promise(function(resolve, reject) {
// Browser.window.setTimeout(resolve, msec);
Timer.delay(() -> {
resolve(1);
}, msec);
});
}
static function main2() {
if (Cluster.isMaster) {
trace("Master");
Cluster.setupMaster();
for (i in 0...16) {
var worker = Cluster.fork();
worker.on('message', function(data:String, f) {
// var dd=Json.stringify(msg);
// trace('worker ' + worker.id + ': ' + msg);
trace(data.length);
JSAsync.jsasync((worker2,data2, f2) -> {
trace('got data =$data2');
timer(1000).jsawait();
worker2.disconnect();//I want to put work here,but not work.
//or //worker.disconnect();//not work
})(worker,data, f);
//worker.disconnect();//this work.
});
}
Cluster.on('exit', function(worker, code, signal) {
trace('worker ' + worker.id + ' died');
});
} else {
trace("worker " + Cluster.worker.id + " started");
var worker = Cluster.worker;
worker.send("hello");
}
}
static function main() {
main2();
}
}
A class constructor can't have a return value
is the error shown by the compiler if you try to assign a return value to new()
.
Is there a way to assign async
to a constructor or is that just not something that's possible even though the JS output might just be yet another function?
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.