Skip to content

Instantly share code, notes, and snippets.

@mygoare
Created August 24, 2016 17:39
Show Gist options
  • Save mygoare/7b42ef2730360cefb567590147f6fffd to your computer and use it in GitHub Desktop.
Save mygoare/7b42ef2730360cefb567590147f6fffd to your computer and use it in GitHub Desktop.
understand co (a simple co)
function co(GenFunc) {
return function(cb) {
var gen = GenFunc()
next()
function next(err, args) {
if (err) {
cb(err)
} else {
if (gen.next) {
var ret = gen.next(args)
if (ret.done) {
// console.log(cb, args);
cb && cb(null, args)
} else {
ret.value(next)
}
}
}
}
}
}
co(function *(){
var res = yield (function(){
return function(cb){
cb(null, 1, 2)
}
})();
console.log('res: ', res); // res: 1
})(function(){
console.log('args: ', arguments); // args: [null, 1]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment