1) What is better streamlinejs: https://github.com/Sage/streamlinejs or narrative: http://www.neilmix.com/narrativejs/ ? any others libs?
2) How does any of those libraries even work? (I read the docs, I am looking for a simplify explanation of what's going on behind the scene..)
As far as question #2....in general these things:
I wrote a partial converter as a learning experience a while back. I used uglify.js to parse into an AST and then the tree walker that lib provides to do the transformations. The transformations were general purpose and produced code that looked like a state machine -- where each step started with a sequence of 0 or more sync actions and ended with an async action. E.g. this simple script:
would get converted to this:
I imagine that streamline and the like do something very similar. Certain structures (loops, try/catch) need special handing but the general approach is the same -- convert into a state machine.
The issues with this approach that I found were:
1) it's not a local problem - i.e. any async behavior that needs to be handled infects everything all the way up the call stack.
2) you need function metadata so you either have to make assumptions or require people to annotate their functions in some manner.