luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
532 stars 29 forks source link

Running in browser? #3

Closed lolive closed 10 years ago

lolive commented 11 years ago

Can we expect Wait.for to run on any browser any time soon? For example when all the major players embrace the latest ECMAScript specifications. Your comment is welcome.

luciotato commented 11 years ago

According to this.. http://stackoverflow.com/questions/18293563/can-node-fibers-be-implemented-using-es6-generators and this... http://calculist.org/blog/2011/12/14/why-coroutines-wont-work-on-the-web/ it doesn't look good.

luciotato commented 11 years ago

It didn't looked good, but it went much better than expected!

Now, the answer is YES.

I'm about to upload a version of _wait.for_ based on the upcoming ES6 Generators. The funny thing is, the implementation of the core function _wait.for(async,arg...)_, using ES6 generators is:

wait.for = function( asyncFn ) { return arguments; }

Yes, just return arguments.

You use _wait.for inside a generator (function) in conjunction with new JS/ES6 _yield* keyword, as in:

var data = yield wait.for ( fs.readFile, '/etc/somefile' );

Surprisingly, ES6 based implementation of wait.for is almost a no-op, you can even completely omit it...

Given that evaluating **_wait.for**_ return its arguments, the call can be replaced with an object literal, which is an array-like object. It results that: ``` javascript wait.for( asyncFn, arg1, arg2 ) // return arguments === {0:asyncFn, 1:arg1, 2:arg2 } // is equivalent to... ~= [ asyncFn, arg1, arg2 ] // is similar to... ``` so, the following two snippets are equivalent (inside a generator launched via **_wait.launchFiber(generator)**_): ``` javascript // call an async function and wait for results, (wait.for syntax): var data = yield wait.for ( fs.readFile, '/etc/somefile', 'utf8' ); console.log(data); // call an async function and wait for results, (fancy syntax): var data = yield [ fs.readFile, '/etc/passwd', 'utf8' ]; console.log(data); ```
luciotato commented 11 years ago

PUBLISHED! new repository Wait.for-ES6

Sequential programming for node.js _and the browser_, end of callback hell.

naggie commented 11 years ago

Great news!

I'm wondering if waitfor can detect if ES6 is available, and load the correct implementation. That would be nice, but do you agree that it's a good idea?

Thanks for this project. I am very pleased that Exceptions can actually be used!

luciotato commented 11 years ago

The ES6-generators impose on the programmer NEW SYNTAX (a mistake in my opinion).

Instead of being able to launch any function as a generator, you need to use a MODIFIED keyword funcion: "function*" (note the asterisk), and you can only use 'yield' inside such functions. So, they're adding two new keywords, and altering "function" instead of adding functionality as core objects and functions.

Because of that, 'waitfor-ES6' does the same as 'waitfor' -BUT- is forced to use 'function*' and 'yield'. The conversion is easy, but you cannot use exactly the same code with both libs.

On Tue, Sep 3, 2013 at 7:22 AM, Callan Bryant notifications@github.comwrote:

Great news!

I'm wondering if waitfor can detect if ES6 is available, and load the correct implementation. That would be nice, but do you agree that it's a good idea?

Thanks for this project. I am very pleased that Exceptions can actually be used!

— Reply to this email directly or view it on GitHubhttps://github.com/luciotato/waitfor/issues/3#issuecomment-23702467 .