Skip to content

Instantly share code, notes, and snippets.

@ahomu
Last active January 2, 2016 13:49
Show Gist options
  • Save ahomu/8312813 to your computer and use it in GitHub Desktop.
Save ahomu/8312813 to your computer and use it in GitHub Desktop.
var co = require('co');
var stack = [
function *first(next) { // second generator
console.log('1 prev');
yield next; // second に delegate
console.log('1 next');
},
function *second(next) { // third generator
console.log('2 prev');
console.log(next.constructor.name);
yield next; // third に delegate
console.log('2 next');
},
function *third(next) { // noop generator
console.log('3 prev');
yield next; // noop に delegate
console.log('3 next');
}
];
co(function *() {
console.log('start');
var prev = function *() {}; // noop generator
var i = stack.length;
while (i--) {
prev = stack[i].call(this, prev)
console.log('#' + (i+1) + '#');
}
yield *prev; // first generator に delegate
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment