jmar777 / suspend

Callback-free control flow for Node using ES6 generators.
547 stars 22 forks source link

wish: named forks #17

Open axkibe opened 10 years ago

axkibe commented 10 years ago

I'd wish I could call fork with names.

var sus = require( 'suspend' )
fs.readFile( 'a.txt', sus.fork( "a" );
fs.readFile( 'b.txt', sus.fork( "b" );
var joi = sus.join();
console.log( joi.a, joi. b);

Would give nice code rather than counting fork

Additionally not an issue but a question. Say I got a generator which wants to runs two generators in parallel - that is starting the second generator when the first one yields. Then waits until both are finished. How'd one approach this?

axkibe commented 10 years ago

BTW: suspend is great! After trying various approaches the last years, I'm currently settling on this :-)

jmar777 commented 10 years ago

@axkibe Happy to hear suspend is working well for you, and thanks for saying so!

I think your idea for named forks is a good one, and I can't think of any other obvious use for a single string parameter to fork(). Let me stew it over for a little bit, but initially I like it.

axkibe commented 10 years ago

I gave some more thought about this, and I think there might be better ways than using join, if its implementable ( i don't know :-) something like this would be awesome:

var
    sus = require( 'suspend' ),
    a = sus( fs.readFile( 'a.txt', sus.callback() ),
    b = sus( fs.readFile( 'b.txt', sus.callback() );

try
{
console.log( yield a.data( ) );
console.log( yield b.data( ) );
}
catch ( err )
{
/// error handler
}