Skip to content

Instantly share code, notes, and snippets.

@meehow
Created August 13, 2015 16:12
Show Gist options
  • Save meehow/68f8865fe84b4b049677 to your computer and use it in GitHub Desktop.
Save meehow/68f8865fe84b4b049677 to your computer and use it in GitHub Desktop.
very basic example of how flow control can be accomplished with generators
var fs = require('fs');
function thread(fn) {
var gen = fn();
function next(err, res) {
var ret = gen.next(res);
if (ret.done) return;
ret.value(next);
}
next();
}
thread(function *() {
var a = yield read('Readme.md');
var b = yield read('package.json');
console.log(a);
console.log(b);
});
function read(path) {
return function(cb) {
fs.readFile(path, 'utf8', cb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment