Created
March 10, 2014 22:47
-
-
Save bevacqua/9476049 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); | |
} | |
} | |
} | |
function get (resume) { | |
setTimeout(function () { | |
resume(null, ['bacon', 'lettuce', 'crispy bacon']); | |
}, 1000); | |
} | |
flow(function* (resume) { | |
console.log('fetching food types...'); | |
var types = yield get; | |
console.log('waiting around...'); | |
yield setTimeout(resume, 2000); | |
console.log(types.join(' ')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment