ES6带来了很多新的特性,其中生成器、yield等能对之前金字塔式的异步回调做到很好地解决,而基于此封装的co框架能让我们完全已同步的方式来编写异步代码。这篇文章就对生成器函数(GeneratorFunction)及框架thunkify、co的核心代码做比较彻底的分析。co的使用还是比较广泛的,除了我们日常的编码要用到外,一些知名框架也是基于co实现的,比如被称为下一代的Nodejs web框架的koa等。
function* func(){}
function test(){
return 'b';
}
function* func(){
var a = yield 'a';
function test(){
return 'b';
}
function* func(){
var a = yield 'a';
console.log('gen:',a);// gen:1
var b = yield test();
console.log('gen:',b);// gen:2
}
var func2 = func();
var a = func2.next();
console.log('next:', a);// next: { value: 'a', done: false }
var b = func2.next(1);
console.log('next:', b);// next: { value: 'b', done: false }
var c = func2.next(2);
console.log('next:', c);// next: { value: undefined, done: true }
function* genFunc(){
yield arguments;
yield* arguments;
}
var gen = genFunc(1,2);
console.log(gen.next().value); // { '0': 1, '1': 2 }
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
function* gen1(){
yield 2;
yield 3;
}
function* gen2(){
yield 1;
yield* gen1();
yield 4;
}
var g2 = gen2();
console.log(g2.next().value); // 1
console.log(g2.next().value); // 2
console.log(g2.next().value); // 3
console.log(g2.next().value); // 4
function readFile(path, encoding){
return function(cb){
fs.readFile(path, encoding, cb);
};
}
那什么叫thunk函数呢?
var co = require('co'),
fs = require('fs'),
Promise = require('es6-promise').Promise;
function readFile(path, encoding){
return function(cb){
fs.readFile(path, encoding, cb);
};
}
co(function* (){// 外面不可见,但在co内部其实已经转化成了promise.then().then()..链式调用的形式
var a = yield readFile('a.txt', {encoding: 'utf8'});
console.log(a); // a
var b = yield readFile('b.txt', {encoding: 'utf8'});
console.log(b); // b
var c = yield readFile('c.txt', {encoding: 'utf8'});
console.log(c); // c
return yield Promise.resolve(a+b+c);
}).then(function(val){
console.log(val); // abc
}).catch(function(error){
console.log(error);
});
var co = require('co'),
thunkify = require('thunkify'),
fs = require('fs'),
Promise = require('es6-promise').Promise;
var readFile = thunkify(fs.readFile);
co(function* (){// 外面不可见,但在co内部其实已经转化成了promise.then().then()..链式调用的形式
var a = yield readFile('a.txt', {encoding: 'utf8'});
console.log(a); // a
var b = yield readFile('b.txt', {encoding: 'utf8'});
console.log(b); // b
var c = yield readFile('c.txt', {encoding: 'utf8'});
console.log(c); // c
return yield Promise.resolve(a+b+c);
}).then(function(val){
console.log(val); // abc
}).catch(function(error){
console.log(error);
});
/**
* Module dependencies.
*/
var assert = require('assert');
/**
* Expose `thunkify()`.
*/
module.exports = thunkify;
/**
* Wrap a regular callback `fn` as a thunk.
*
* @param {Function} fn
* @return {Function}
* @api public
*/
function thunkify(fn) {
assert('function' == typeof fn, 'function required');
// 返回一个包含thunk函数的函数,返回的thunk函数用于执行yield,而外围这个函数用于给thunk函数传递参数
return function() {
var args = new Array(arguments.length);
// 缓存当前上下文环境,给fn提供执行环境
var ctx = this;
// 将参数类数组转化为数组(实现方式略显臃肿,可直接用Array.prototype.slice.call(arguments)实现)
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
// 真正的thunk函数(有且只有一个参数是callback的函数,且callback的第一个参数为error)
// 类似于:
// function(cb) {fs.readFile(path, {encoding: 'utf8}, cb)}
return function(done) {
var called;
// 将回调函数再包裹一层,避免重复调用;同时,将包裹了的真正的回调函数push进参数数组
args.push(function() {
if (called) return;
called = true;
done.apply(null, arguments);
});
try {
// 在ctx上下文执行fn(一般是异步函数,如:fs.readFile)
// 并将执行thunkify之后返回的函数的参数(含done回调)传入,类似于执行:
// fs.readFile(path, {encoding: 'utf8}, done)
// 关于done是做什么用,则是在co库内
fn.apply(ctx, args);
} catch (err) {
done(err);
}
}
}
};
/**
* slice() reference.
*/
var slice = Array.prototype.slice;
/**
* Expose `co`.
*/
module.exports = co['default'] = co.co = co;
/**
* Wrap the given generator `fn` into a
* function that returns a promise.
* This is a separate function so that
* every `co()` call doesn't create a new,
* unnecessary closure.
*
* @param {GeneratorFunction} fn
* @return {Function}
* @api public
*/
co.wrap = function(fn) {
createPromise.__generatorFunction__ = fn;
return createPromise;
function createPromise() {
return co.call(this, fn.apply(this, arguments));
}
};
/**
* Execute the generator function or a generator
* and return a promise.
*
* @param {Function} fn
* @return {Promise}
* @api public
*/
// gen必须是一个生成器函数(会执行该函数并返回生成器)或者是一个生成器(generator函数的返回值)
function co(gen) {
// 记录上下文环境
var ctx = this;
// 除gen之外的其他参数
var args = slice.call(arguments, 1)
// we wrap everything in a promise to avoid promise chaining,
// which leads to memory leak errors.
// see https://github.com/tj/co/issues/180
// 返回一个Promise实例,所以可以以下面这种方式调用co:
/**
* co(function*(){}).then(function(val){
*
* });
* */
return new Promise(function(resolve, reject) {
// 如果gen是一个函数则将其置为函数的返回值
if (typeof gen === 'function') {
gen = gen.apply(ctx, args);
}
// 如果gen不是生成器,则直接返回
if (!gen || typeof gen.next !== 'function') {
return resolve(gen);
}
// 核心方法,启动generator的执行
onFulfilled();
/**
* @param {Mixed} res
* @return {Promise}
* @api private
*/
// res记录的是:上一个yield的返回值中value的值({done:false,value:''}中value的值)
// ret记录的是:本次yield的返回值(整个{done:false,value:''})
// generator相关:执行生成器的next()方法的时候,会在当前yield处执行完毕并停住,
// next()方法返回的是yield执行后的状态(done)及yield 表达式返回的值(value),
// 而next()方法内的参数会作为:var a=yield cb();a的值,所以往下看
/**
* 假设:co(function*(){
* var a = yield readFile('a.txt');
* console.log(a);
* var b = yield readFile('b.txt);
* console.log(b);
* });
* 那么根据上面generator的理论,res就是a,b的值
* */
function onFulfilled(res) {
var ret;
try {
// 返回的是co里yield后面表达式的值。如果co里yield的是thunk函数那ret.value就是thunk函数
ret = gen.next(res);
} catch (e) {
return reject(e);
}
next(ret);
}
/**
* @param {Error} err
* @return {Promise}
* @api private
*/
function onRejected(err) {
var ret;
try {
ret = gen.throw(err);
} catch (e) {
return reject(e);
}
next(ret);
}
/**
* Get the next value in the generator,
* return a promise.
*
* @param {Object} ret
* @return {Promise}
* @api private
*/
function next(ret) {
// 执行完毕的话,如果外层调用的是:
/**
* co(function*(){
* return yield Promise.resolve(1);
* }).then(function(val){
* console.log(val); // 1
* });
* */
// 则ret.value就是上面传递到then成功回调里val的值
if (ret.done) {
return resolve(ret.value);
}
// 还没结束的话将ret.value转化为Promise实例,相当于执行:
// promise.then(onFulfilled).then(onFulfilled).then(onFulfilled)...
var value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) {
// 此时onFulfilled里参数传入的就是上一个yield的返回值的value值
return value.then(onFulfilled, onRejected);
}
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, ' + 'but the following object was passed: "' + String(ret.value) + '"'));
}
});
}
/**
* Convert a `yield`ed value into a promise.
*
* @param {Mixed} obj
* @return {Promise}
* @api private
*/
function toPromise(obj) {
if (!obj) return obj;
if (isPromise(obj)) return obj;
if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj);
if ('function' == typeof obj) return thunkToPromise.call(this, obj);
if (Array.isArray(obj)) return arrayToPromise.call(this, obj);
if (isObject(obj)) return objectToPromise.call(this, obj);
return obj;
}
/**
* Convert a thunk to a promise.
*
* @param {Function}
* @return {Promise}
* @api private
*/
function thunkToPromise(fn) {
var ctx = this;
return new Promise(function(resolve, reject) {
fn.call(ctx, function(err, res) {
if (err) return reject(err);
if (arguments.length > 2) res = slice.call(arguments, 1);
resolve(res);
});
});
}
/**
* Convert an array of "yieldables" to a promise.
* Uses `Promise.all()` internally.
*
* @param {Array} obj
* @return {Promise}
* @api private
*/
function arrayToPromise(obj) {
return Promise.all(obj.map(toPromise, this));
}
/**
* Convert an object of "yieldables" to a promise.
* Uses `Promise.all()` internally.
*
* @param {Object} obj
* @return {Promise}
* @api private
*/
function objectToPromise(obj) {
var results = new obj.constructor();
var keys = Object.keys(obj);
var promises = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var promise = toPromise.call(this, obj[key]);
if (promise && isPromise(promise)) defer(promise, key);
else results[key] = obj[key];
}
return Promise.all(promises).then(function() {
return results;
});
function defer(promise, key) {
// predefine the key in the result
results[key] = undefined;
promises.push(promise.then(function(res) {
results[key] = res;
}));
}
}
/**
* Check if `obj` is a promise.
*
* @param {Object} obj
* @return {Boolean}
* @api private
*/
function isPromise(obj) {
return 'function' == typeof obj.then;
}
/**
* Check if `obj` is a generator.
*
* @param {Mixed} obj
* @return {Boolean}
* @api private
*/
function isGenerator(obj) {
return 'function' == typeof obj.next && 'function' == typeof obj.throw;
}
/**
* Check if `obj` is a generator function.
*
* @param {Mixed} obj
* @return {Boolean}
* @api private
*/
function isGeneratorFunction(obj) {
var constructor = obj.constructor;
if (!constructor) return false;
if ('GeneratorFunction' === constructor.name || 'GeneratorFunction' === constructor.displayName) return true;
return isGenerator(constructor.prototype);
}
/**
* Check for plain object.
*
* @param {Mixed} val
* @return {Boolean}
* @api private
*/
function isObject(val) {
return Object == val.constructor;
}
var co = require('co'),
thunkify = require('thunkify'),
fs = require('fs'),
Promise = require('es6-promise').Promise;
function readFile(path, encoding){
return function(cb){
fs.readFile(path, encoding, cb);
};
}
//var readFile = thunkify(fs.readFile);
co(function* (){// 外面不可见,但在co内部其实已经转化成了promise.then().then()..链式调用的形式
var a = yield readFile('a.txt', {encoding: 'utf8'});
console.log(a); // a
var b = yield readFile('b.txt', {encoding: 'utf8'});
console.log(b); // b
var c = yield readFile('c.txt', {encoding: 'utf8'});
console.log(c); // c
return yield Promise.resolve(a+b+c);
}).then(function(val){
console.log(val); // abc
}).catch(function(error){
console.log(error);
});
// 记录上下文环境
var ctx = this;
// 除gen之外的其他参数
var args = slice.call(arguments, 1)
// we wrap everything in a promise to avoid promise chaining,
// which leads to memory leak errors.
// see https://github.com/tj/co/issues/180
// 返回一个Promise实例,所以可以以下面这种方式调用co:
/**
* co(function*(){}).then(function(val){
*
* });
* */
return new Promise(function(resolve, reject) {
});
if (typeof gen === 'function') {
gen = gen.apply(ctx, args);
}
首先,判断co()函数的第一个参数是否是函数,是的话将除gen之外的参数传给该函数并返回给gen;在这里因为gen是一个生成器函数,所以返回一个生成器;
if (!gen || typeof gen.next !== 'function') {
return resolve(gen);
}
后面判断如果gen此时不是一个生成器,则直接执行Promise的resolve,其实就是将gen传回给:co().then(function(val){});里的val了;
onFulfilled();
function onFulfilled(res) {
var ret;
try {
ret = gen.next(res);
} catch (e) {
return reject(e);
}
next(ret);
}
var a = yield readFile('a.txt', {encoding: 'utf8'});
那么ret是一个对象,大概是这样:
{
done: false,
value: function(cb){
fs.readFile(path, encoding, cb);
}
}
function next(ret) {
if (ret.done) {
return resolve(ret.value);
}
var value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) {
return value.then(onFulfilled, onRejected);
}
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, ' + 'but the following object was passed: "' + String(ret.value) + '"'));
}
function toPromise(obj) {
if (!obj) return obj;
if (isPromise(obj)) return obj;
if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj);
if ('function' == typeof obj) return thunkToPromise.call(this, obj);
if (Array.isArray(obj)) return arrayToPromise.call(this, obj);
if (isObject(obj)) return objectToPromise.call(this, obj);
return obj;
}
/**
* Convert a thunk to a promise.
*
* @param {Function}
* @return {Promise}
* @api private
*/
function thunkToPromise(fn) {
var ctx = this;
return new Promise(function(resolve, reject) {
fn.call(ctx, function(err, res) {
if (err) return reject(err);
if (arguments.length > 2) res = slice.call(arguments, 1);
resolve(res);
});
});
}
if (value && isPromise(value)) {
return value.then(onFulfilled, onRejected);
}
其实也就是onFulFilled的参数res。根据上面第三条执行准则,我们知道,res是被传入到生成器的next()方法里的,其实也就是对应co内生成器函数参数里的var a = yield readFile('a.txt',{encoding:'utf8'});里的a的值,从而实现了类似于同步的变成范式。
function co(generator) {
return function(fn) {
var gen = generator();
function next(err, result) {
if(err){
return fn(err);
}
var step = gen.next(result);
if (!step.done) {
step.value(next);
} else {
fn(null, step.value);
}
}
next();
}
}
var co = require('./co');
// wrap the function to thunk
function readFile(filename) {// 辅助传参,yield真正使用的是其返回的thunk函数
return function(callback) {
require('fs').readFile(filename, 'utf8', callback);
};
}
co(function * () {
var file1 = yield readFile('./file/a.txt');
var file2 = yield readFile('./file/b.txt');
console.log(file1);
console.log(file2);
return 'done';
})(function(err, result) {
console.log(result)
});
content in a.txt
content in b.txt
done