async library

429 views
Skip to first unread message

Yanis Benson

unread,
Jan 4, 2013, 10:26:58 AM1/4/13
to haxe...@googlegroups.com
Hello, fellow haxers.

I was working with node.js for some time, and that forced me to dive into asynchronous programming. Not just calling async function time to time, but doing that all around. As many others, I found the resulting code not that understandable and not that beautiful. I tried various abstractions, but none of them fully satisfied me. So I figured I can use haxe macros to make my asynchronous code nice-looking(and even strip some overhead you get by using any of async abstractions).

The resulting precompiler proved to be quite useful for me. More than that, it gave me ability to use some very complex logic I was totally unable to write in pure asynchronous code. (Well, I actually was able, but the resulting code looked like spaghetti plate in 2 minutes.)

So I put all that together and now am releasing it as library. It isn't really all that finished and polished, but satisfies my current needs. It needs some more testing though.

It is relatively hard to say what it does in a couple of words, but the point is to just mark asynchronous calls as async and think of it as synchronous calls. Just look at code: https://github.com/stroncium/hx-async/blob/master/test/Test.hx . Note that this example don't do anything special, but actual code may have all the construction nested, resulting in quite a complex things, to the point it takes hours to understand generated code(true story, I spent many hours on that while fixing some bugs). That is the cause of library being not so well-tested. As far as I can see it should work good, but errors may arrive from some complex patterns.

Library is available as 'async' in haxelib: http://lib.haxe.org/p/async .

Some more deep documentation can be found here: https://github.com/stroncium/hx-async

Any comments, bug reports(use https://github.com/stroncium/hx-async/issues , please) or ideas are appreciated.

Oh, and happy New Year!

  ===  Best regards, Yanis.

Cauê Waneck

unread,
Jan 4, 2013, 10:34:29 AM1/4/13
to haxe...@googlegroups.com
looks great, Yanis! Thanks!


2013/1/4 Yanis Benson <yanis....@gmail.com>

Cauê Waneck

unread,
Jan 4, 2013, 10:37:48 AM1/4/13
to haxe...@googlegroups.com
How do you integrate error handling wrt Node.JS? Also/related, how does it handle callback functions that need more than one argument?


2013/1/4 Cauê Waneck <wan...@gmail.com>

Yanis Benson

unread,
Jan 4, 2013, 11:25:31 AM1/4/13
to haxe...@googlegroups.com
Errors are passed as it is. Same with arguments. The best place to find answers to your questions will be https://github.com/stroncium/hx-async/blob/master/test/Test.hx . However, to understand how the conversion works I'd recommend you to compile this chunk of code and look at the generated code

static function randomAsyncFunction(arg1, arg2, cb){

Yanis Benson

unread,
Jan 4, 2013, 11:27:26 AM1/4/13
to haxe...@googlegroups.com
Ooops, autosubmit...

static function randomAsyncFunction(arg1, arg2, cb){
 cb(null, 1, '2');
}

@async static function test(){
  async(v1, v2 = randomAsyncFunction(1, 2));
}


Also, I guess I forgot to update documentation part related to direct assigns, I'll look into it.

j...@justinfront.net

unread,
Jan 4, 2013, 11:27:50 AM1/4/13
to haxe...@googlegroups.com
Sounds good.

Did you look at winRT and how it's async stuff works?  I know that they have a keyword but not sure if it would apply to any win8 js code or if it relates to the TypeScript code they use for IE10 js winRT specific stuff. 

Best Justin

Yanis Benson

unread,
Jan 4, 2013, 11:59:28 AM1/4/13
to haxe...@googlegroups.com
I wasn't looking into it before(I don't generally work with any microsoft solutions.), but from what  understood it is just alow-level model asynchronous calls. On higher level, it works the same, and you actually can use async library to work with winRT. And from the code samples I've seen, simple winRT have the same problems as you get in node.js for example. I even had seen some attempts to do the same async call abstractions as ones you can find for JavaScript. Maybe I'm missing something?

Janek Priimann

unread,
Jan 4, 2013, 1:57:11 PM1/4/13
to haxe...@googlegroups.com
Nice!

But why did you choose GPL for the license?

Janek

Yanis Benson

unread,
Jan 4, 2013, 11:55:54 PM1/4/13
to haxe...@googlegroups.com
Github must be tricking me. My license of choice is DWTFYWT, I'll add it when will be near my netbook.

Yanis Benson

unread,
Jan 6, 2013, 2:37:28 PM1/6/13
to haxe...@googlegroups.com
MIT license added.

Also, seems like I fixed a nasty bug, thanks to Alex Hoyau (github) .


On Friday, January 4, 2013 10:57:11 PM UTC+4, Janek Priimann wrote:

杨博

unread,
Jan 10, 2013, 5:34:53 AM1/10/13
to haxe...@googlegroups.com
Have you took a look at old discussion or my implementation?

在 2013年1月4日星期五UTC+8下午11时26分58秒,Yanis Benson写道:

Yanis Benson

unread,
Jan 18, 2013, 6:18:18 AM1/18/13
to haxe...@googlegroups.com
Yeah, I've seen your implementation some time ago. But things like "var err, written = Node.fs.write(fd, content, totalWritten, content.length - totalWritten, null).async();" seemed totally unobvious to me. I mean, it works of course, but the code is misleading when you read it. While for me, one of the main ideas was to make code readable, since most of asynchronous code isn't hard to write but hard to read and fix.

Another important thing for me is error passing. In my production code for node.js I have to check for errors on every 3rd line sometimes. (I'm currently in a process of porting it to haxe and async, and I can say that just by removing all this check I made my code much simpler and around 1/3 shorter I guess.) I've made a gist showing implementation of a test from your library when one uses async. https://gist.github.com/4563919 Much simpler IMO. Though the function parallelExec I used isn't a part of library yet. It's easy to write, but I' thinking about some macro sugar so it wont be that many lines of supporting code.

杨博

unread,
Jan 21, 2013, 9:36:25 AM1/21/13
to haxe...@googlegroups.com
My project provide only one magic word: async(). My users reuse for, if, while, switch, and all other Haxe keywords as usual, with the magic async().

Unfortunately, only wizards can understand the magic. The other guys feel totally unobvious on the magic. To feel safe, they invent dozen of control structures: parallelExecAsync.itMonad.dO<= ret, etc.

Are you a wizard or a muggle?

在 2013年1月18日星期五UTC+8下午7时18分18秒,Yanis Benson写道:

Yanis Benson

unread,
Jan 21, 2013, 10:31:52 AM1/21/13
to haxe...@googlegroups.com
I guess you wasn't reading any of code I gave to you. Only two magic
words are used: "async" and "parallel". And I doubt later can be
removed if we want fast parallel calls. Anyhow, I'd introduce 2000
more magic words if it only made code more readable.

On Mon, Jan 21, 2013 at 6:36 PM, 杨博 <pop....@gmail.com> wrote:
> My project provide only one magic word: async(). My users reuse for, if,
> while, switch, and all other Haxe keywords as usual, with the magic async().
>
> Unfortunately, only wizards can understand the magic. The other guys feel
> totally unobvious on the magic. To feel safe, they invent dozen of control
> structures: parallelExec, Async.it, Monad.dO, <= ret, etc.
>
> Are you a wizard or a muggle?

--

Best regards, Yanis.

Atry

unread,
Jan 21, 2013, 10:43:34 AM1/21/13
to haxe...@googlegroups.com
Good for you.


2013/1/21 Yanis Benson <yanis....@gmail.com>

--

    Best regards, Yanis.

Reply all
Reply to author
Forward
0 new messages