Skip to content

Instantly share code, notes, and snippets.

@spion
Forked from creationix/output.log
Last active December 17, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/5621488 to your computer and use it in GitHub Desktop.
Save spion/5621488 to your computer and use it in GitHub Desktop.
var fs = require('fs');
function run(makeGenerator) {
return function () {
var generator = makeGenerator.apply(this, arguments);
var continuable, sync, value;
next();
function next() {
while (!(continuable = generator.send(value)).done) {
continuable = continuable.value;
sync = undefined;
continuable(callback);
if (sync === undefined) {
sync = false;
break;
}
}
}
function callback(err, val) {
if (err) return generator.throw(err);
value = val;
if (sync === undefined) {
sync = true;
}
else {
next();
}
}
}
}
run(function* () {
console.log((yield fs.readFile.bind(fs, __filename)).toString());
console.log("Done");
})();
nvm use v0.11.2-generators
node --harmony gen-test.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment