Skip to content

Instantly share code, notes, and snippets.

@bevacqua
Created March 10, 2014 20:32
Show Gist options
  • Save bevacqua/9473675 to your computer and use it in GitHub Desktop.
Save bevacqua/9473675 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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment