Skip to content

Instantly share code, notes, and snippets.

@bevacqua
Created March 10, 2014 22:47
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 bevacqua/9476049 to your computer and use it in GitHub Desktop.
Save bevacqua/9476049 to your computer and use it in GitHub Desktop.
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