co代码贴过来居然保存不了,只能图片了github:qingdie-co
var co = require('utils/co.js');
co(function* (cb) {
var user = yield getUser('你好!', cb);
console.log(user);
var user1 = yield getUser1('棒棒哒!');
console.log(user1);
var ret = yield co(function (cba) {
getUser('棒棒哒!', cba('user'));
getUser('你好!', cba('user1'));
})(2);
console.log(ret);
});
function co(genFunc, cb) {
if (co.isGenerator(genFunc)) {
genFunc = genFunc(next);
next();
} else if (co.isGeneratorObj(genFunc)) {
next();
}
else if (co.isFunction(genFunc)) {
return function (num) {
return new Promise(function (res) {
var data = {};
var cbnum = 0;
genFunc(function (key) {
return num == 1 ? res(key) : function (args) {
data[key] = args;
++cbnum == num && res(data);
}
});
});
}
}