Skip to content

Instantly share code, notes, and snippets.

@bevacqua
Created March 10, 2014 20:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bevacqua/9473704 to your computer and use it in GitHub Desktop.
Save bevacqua/9473704 to your computer and use it in GitHub Desktop.
function flow (generator) {
var iterator = generator(next);
next();
function next (err, result) {
if (err) iterator.throw(err);
var item = iterator.next(result);
if (item.done) return;
if (typeof item.value === 'function') item.value(next);
}
}
// hacks, disregard these
var fs = {
readFile:function(a,b,c){
setTimeout(function(){
console.log('foo', a);
c();
},1000);
}
};
function read (path) {
return function (done) {
fs.readFile(path, 'utf8', done);
}
}
// usage!
flow(function* (next) {
var a = yield read('Readme.md');
var b = yield read('package.json');
console.log('wait for it...');
var c = yield setTimeout(next, 1000);
console.log(a);
console.log(b);
});
@fixe
Copy link

fixe commented Mar 10, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment