Created
August 13, 2015 16:12
-
-
Save meehow/68f8865fe84b4b049677 to your computer and use it in GitHub Desktop.
very basic example of how flow control can be accomplished with generators
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
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