Created
July 23, 2013 13:48
-
-
Save wemakeweb/6062465 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
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