Created
March 10, 2014 20:32
-
-
Save bevacqua/9473675 to your computer and use it in GitHub Desktop.
This file contains 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
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