function compose(middleware) {
return function *(next) {
var i = middleware.length;
var prev = next || noop ();
var curr;
while (i--) {
curr = middleware[1];
prev = curr.call(this, prev); //
}
yield *prev;
}
}
看下例的运行效果
12345678910111213141516171819
var co = require('co');
var stack = [];
var arr = [];
stack.push(function *(next) {
arr.push(1);
yield next;
arr.push(3);
});
stack.push(function *(next){
arr.push(2);
yield next;
arr.push(4)
});
co(compose(stack)) (function (err) {
if (err) throw err;
console.log(arr); // 输出 [1, 2, 3, 4], 从输出可以看到其执行顺序
});