streamlinejs looks cool

22 views
Skip to first unread message

Stone Zhong

unread,
Feb 23, 2016, 5:07:44 AM2/23/16
to streamline.js
Hi there,

I have been writing async code recently, and even with Promise, the code still looks like pyramid.

I wasn't aware other people feel the same problem and there are bunch of solutions like streamlinejs out there already, so I have developed a tool called 'Async Compiler' (https://github.com/stonezhong/async-compiler) , it looks very similar to streamlinejs, my goal is:

* function annotated with /** @async **/ are async function that returns promise
* function annotated with /** @async **/ can call other async functions just like calling sync functions, it can pass promise(or thenable) to the function as argument and the tool will help to resolve these arguments.
* use try - catch - finally to handle exception like sync function, it will capture rejected promise during execution flow. (not implemented yet)
* throw exception for /** @async **/ function will cause the function to reject the returned promise with throwed exception. (not implemented yet)

Today, I saw this streamlinejs tool, I am very excited since people are already doing very similar thing. I wish in the future async programming in javascript becomes much simpler.

Thanks,
Stone





Andrew Bradley

unread,
Feb 23, 2016, 11:43:23 AM2/23/16
to stream...@googlegroups.com
You can look at Typescript, Babel, and the ECMAScript spec for async functions.  Babel and Typescript are compilers that already implement async functions.  Microsoft Edge's Chakra engine natively supports async functions as well.

--Andrew

--
You received this message because you are subscribed to the Google Groups "streamline.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to streamlinejs...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stone Zhong

unread,
Feb 24, 2016, 5:28:44 AM2/24/16
to streamline.js
Thank! I actually tried async/await proposed for ES7 with babel, it does not seems working for me in some cases, here is an example:

The code below prints 'undefined' instead of 'SDE'
"use strict";
require('babel-polyfill');

var fieldName = Promise.resolve('title');
var person = Promise.resolve({title: Promise.resolve('SDE')});

async
function foo() {
  console
.log(await (await person)[await fieldName]);
}

foo
();



However, with /** @async **/, the code below produce the right output
var AsyncTool = require('async-compiler-runtime');

var fieldName = Promise.resolve('title');
var person = Promise.resolve({title: Promise.resolve('SDE')});

/** @async **/ function foo() {
  console
.log(person[fieldName]);
}

foo
();



It could be a implementation bug in babel though.

Thanks,
Stone

Andrew Bradley

unread,
Feb 24, 2016, 1:49:44 PM2/24/16
to stream...@googlegroups.com
You're right; looks like a bug in regenerator.  I modified your example to be a generator instead of an async function, and the same problem happens.  Lines 20 to 22 of the generated code appear to be the culprit.

--Andrew

Bruno Jouhier

unread,
Feb 24, 2016, 6:21:15 PM2/24/16
to stream...@googlegroups.com
Interesting.Here is the streamline version:

"use strict";

var delay = (_, value) => { console.log("delay " + value); setTimeout(_, 1000); return value; }

var fieldName = (_) => delay(_, "title");

var person = (_) => delay(_, { title: delay(_, "SDE") });

console
.log(person(_)[fieldName(_)]);



I get the following results:

$ _node --runtime fibers test._js
delay SDE
delay
[object Object]
delay title
SDE

$ _node
--runtime generators test._js
delay SDE
delay
[object Object]
delay title
SDE

$ _node
--runtime callbacks test._js
delay SDE
delay
[object Object]
delay title
undefined


The fibers and generators mode don't use regenerator. They work fine.


The callbacks mode uses regenerator. It is buggy.


The original streamline (1.0) used a different algorithm to transpile in callbacks mode. I switched to regenerator when I upgraded to babel but it looks like it introduced a bug. I did not notice it because I use the fibers mode on node.js and generators in the browser.


Bruno

Andrew Bradley

unread,
Feb 24, 2016, 6:29:15 PM2/24/16
to stream...@googlegroups.com
Oh right, I forgot you made it a Babel plugin!  Very cool.

I sent a PR to regenerator that adds a failing unit test for this bug.  https://github.com/facebook/regenerator/pull/235

On Wed, Feb 24, 2016 at 6:21 PM, Bruno Jouhier <bjou...@gmail.com> wrote:
Interesting.Here is the streamline version:

``` javascript
"use strict";

var delay = (_, value) => { console.log("delay " + value); setTimeout(_, 1000); return value; }

var fieldName = (_) => delay(_, "title");

var person = (_) => delay(_, { title: delay(_, "SDE") });

console.log(person(_)[fieldName(_)]);
```
I get the following results:
``` sh

Bruno Jouhier

unread,
Feb 24, 2016, 6:38:58 PM2/24/16
to streamline.js
Yep. Babel is really cool. ES6 too!

Thanks for the link. I'll track it and update the streamline dependency when it is fixed.
To unsubscribe from this group and stop receiving emails from it, send an email to streamlinejs+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages