Created
August 24, 2016 17:39
-
-
Save mygoare/7b42ef2730360cefb567590147f6fffd to your computer and use it in GitHub Desktop.
understand co (a simple co)
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 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