COMMUNITY

Status of @await in Haxe

nodejs
async

(François Barbut) #1

Hi there,

Does anyone have some infos about the usage of @await in Haxe ?

For instance, with nodejs target, is it possible to read a file , or perform a sequelize query in a synchronous style with @await ?

I’ve noticed the existence of https://lib.haxe.org/p/tink_await/ , but it’s limited to tink_* libs …


(Aleksandr Kuzmenko) #2

@nadako was trying to implement it. Maybe he can share some details.


(Dan Korostelev) #3

Yes, I was working on a proposal for generic coroutines which can be used to implement await among other things and I still think this is a way to go, although error handling might be something to think about some more. I experimented with implementing the state machine transformation using the control flow graph that compiler is already building internally, but never finished a working prototype due to serious lack of time recently. Maybe it’ll be more realistic when HF gets a new full-time compiler developer, I’ll be happy to help!


(Dmitry Hryppa) #4

I’m using a little bit tricky workaround for NodeJS, which can be replaced with macros via @async and @await metas I think:
https://try.haxe.org/#6f2C9


(Jeff Ward) #5

I’m using haxe-continuation – which I’ve found to work really well. It’s a single-purpose macro (instead of the big tink library), and can automatically wrap e.g. extern JS functions which take a callback as their last parameter. Note that the license is not MIT.

I’m going to talk about using it in my upcoming Haxe Summit talk.

It’s a slick macro that actually breaks your “synchronously-written functions” into multiple functions and callbacks, while maintaining scoped variables. Basically it’s re-writing your synchronous code as a set of asynchronous functions. It also has the interesting side effect of being able to assign multiple variables (because a callback function – which this is translated into – can take multiple parameters):

It’s also awesome that Haxe macros enable this kind of power.

It’s not perfect, though. It doesn’t provide any error handling options (e.g. optionally insert try/catch), and there aren’t any comments. :slight_smile: But, it works for now.


(Marcelo Serpa) #6

Hi Jeff,

So there’s no equivalent of JS’ Promise.prototype.catch approach?