Last active
August 29, 2015 13:58
-
-
Save getify/10172207 to your computer and use it in GitHub Desktop.
using asynquence's `runner(..)` plugin to do async CSP-style (message-passing) generator co-routines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// just a silly number generator that's promise-async | |
function getVal() { | |
return new Promise( function(resolve,reject){ | |
setTimeout( function(){ | |
if (!getVal.__number) getVal.__number = 0; | |
resolve( getVal.__number += 10 ); | |
}, 100 ); | |
} ); | |
} | |
// ********************** | |
ASQ( 2, 3 ) | |
.runner( | |
// machine 1 | |
function*(token){ | |
token.messages; // [2, 3] | |
token.messages[0] += yield getVal(); // pauses generator while promise resolves | |
token.messages[1] += yield getVal(); // pauses generator while promise resolves | |
token.messages.push( token.messages[0] + token.messages[1] ); | |
token.messages; // [12, 23, 35] | |
yield token; // hand control over to next machine | |
token.messages; // [ 100 ] | |
token.messages[0] += yield getVal(); // pauses generator while promise resolves | |
// implicit finish means transfer control to next machine | |
}, | |
// machine 2 | |
function*(token){ | |
token.messages; // [12, 23, 35] | |
token.messages = [ token.messages[0] + token.messages[1] + token.messages[2] ]; | |
token.messages; // [ 70 ] | |
token.messages[0] += yield getVal(); // pauses generator while promise resolves | |
token.messages; // [ 100 ] | |
yield token; // hand control over to next machine | |
token.messages; // [ 140 ] | |
token.messages.push( "Hello World" ); | |
yield token.messages; | |
} | |
) | |
.val( function(msgs){ | |
console.log( msgs[0], msgs[1] ); // 140 "Hello World" | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, I got it working after all. I don't know if this is an idiomatic translation, but it gets the job done.
This is written in typescript, so there is some extra type fluff that you can (hopefully) ignore.
I was particularly curious what the best way was to translate
token.messages[0] += yield getVal();
I ended up with:
Not sure if that's the cleanest or best way to do this.
Here's the full code: