Jscex still does preprocessing (the Jscex.compile call). It just does
it on the fly.
Streamline works the same way, but the compile directive is hidden at
the top of the module.
// magic autocompile directive
if (!require('streamline/module')(module)) return;
// serial concat
function concatAsync(path1, path2, _) {
console.log(fs.readFile(path1, 'utf8', _) + fs.readFile(path2,
'utf8', _));
}
// trivial wrapper to get 'futures' around fs.readFile
function readFile(path1, encoding, _) { return fs.readFile(path,
encoding, _); }
// parallel concat
function concatAsyncParallel(path1, path2, _) {
var rd1 = readFile(path1, 'utf8');
var rd2 = readFile(path2, 'utf8');
console.log(rd1(_) + rd2(_));
}
On Feb 11, 7:02 am, "Jeffrey Zhao" <[email protected]> wrote:
> Jscex (https://github.com/JeffreyZhao/jscex) is easy to switch to harmony:
>
> var concatAsync = Jscex.compile("async", function(path1, path2) {
> // read first file
> var file1 = $await(fs.readFileAsync(path1, 'utf8'));
>
> // read second file
> var file2 = $await(fs.readFileAsync(path2, 'utf8'));
>
> console.log(file1 + file2);
>
> }));
>
> You can even make it parallel easily.
>
> var concatAsync = Jscex.compile("async", function(path1, path2) {
>
> var data = $await(Task.whenAll(
> "file1": fs.readFileAsync(path1, 'utf8'),
> "file2": fs.readFileAsync(path2, 'utf8')));
>
> console.log(data.file1 + data.file2);
>
> }));
>
> No preprocessor, no external compilation, just run directly with any ES3
> engines.
>
> The doc in English is outdated but the samples
> (https://github.com/JeffreyZhao/jscex/tree/master/samples/async) are always
> fine:
>
> Jeffrey Zhao
> Blog:http://blog.zhaojie.me/
> Twitter: @jeffz_cn (Chinese) | @jeffz_en (English)
>
>
>
>
>
>
>
> -----Original Message-----
> From: Chris Scribner
> Sent: Saturday, February 11, 2012 1:01 PM
> To: nodejs
> Subject: [nodejs] Re: node-fibers or generators
>
> Hi, I'm the author of a fibers abstraction called asyncblock.
> (https://github.com/scriby/asyncblock).
>
> I read over the details of harmony generators, and I can tell that
> existing fibers based solutions will not be able to transparently
> switch over to generators. There are two main problems:
>
> 1. The function needs to have an asterisk after it (not a huge deal)
> 2. The existing syntax can't work directly as you must use yield
> directly from the generator function. It's not like fibers where we
> can yield further up the call stack of a function running in a fiber
>
> So, I think the only reasonable solutions for long term planning and
> avoiding CPS style code involve syntax transformation. Asyncblock has
> beta support for syntax rewriting, and I plan on basing it off
> generators when they become available in v8.
>
> The syntax rewriting lets you write code like
>
> asyncblock(function(flow){
> //defer makes the first access to file1 yield until it's ready
> var file1 = fs.readFile(path, 'utf8').defer();
>
> //sync makes the fiber yield until the result is obtained
> var file2 = fs.readFile(path, 'utf8').sync();
>
> console.log(file1 + file2);
> });
>
> Right now that code compiles to use fibers. There's plenty of good CPS
> transforms out there of you want to avoid fibers for now, or of you
> need windows or browser support (be careful about losing line numbers
> though).
>
> Good luck making your decision,
>
> Chris
>
> On Feb 10, 1:17 pm, "john.tiger" <[email protected]> wrote:
> > for a project we do not want lots of nested callbacks so
> > 1) where is node in regards to using harmony generators ?
> > 2) is node-fibers a fork or add on module ?
> > 3) if we use node-fibers and then later generators becomes "standard" -
> > would there be much rewriting required ?
>
> --
> Job Board:http://jobs.nodejs.org/
> Posting
> guidelines:https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group
> athttp://groups.google.com/group/nodejs?hl=en?hl=en
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en