看了下koa的中间件 实现大概是这样个思路。
var co = require(‘co’); var list = [];
var s = function*(next){ console.log(1); yield next; console.log(6); }
var a = function*(next){ console.log(2); yield next; console.log(5); } var b = function*(next){ console.log(3); yield next; console.log(4); }
list.push(s,a,b);
var com = function*(){ var i = list.length; var prev = function *(){},curr;
while(i--){
curr = list[i];
prev = curr.call(this,prev);
}
yield* prev;
}
co(com).catch(function(e){console.log(e)});