Skip to content

Instantly share code, notes, and snippets.

@wemakeweb
Created July 23, 2013 13:48
Show Gist options
  • Save wemakeweb/6062465 to your computer and use it in GitHub Desktop.
Save wemakeweb/6062465 to your computer and use it in GitHub Desktop.
/*
requires node version v0.11.2
call this with `node --harmony thisFile.js`
*/
/*
calls 'cb' after some sleep
time with a random value.
*/
var producer = function(cb ){
setTimeout(function(){
cb(false, Math.random());
}, Math.random() * 1000);
};
/*
generates the generator function 'gen'
and provides the `next` function as helper
to the generator
*/
function Helper( gen ){
var a = gen( next );
a.next();
function next (err, value){
a.send(value);
};
};
/*
lets test that shit
*/
Helper(function*( next ){
var x = yield producer( next );
var y = yield producer( next );
console.log(x, y);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment