651690
koa2.0.0中,在application.js里有个callback函数
callback() {
const fn = compose(this.middleware);
/*...*/
return (req, res) => {
/*...*/
fn(ctx).then(() => respond(ctx)).catch(ctx.onerror);
};
}
其中用到了koa-compose的compose函数
function compose(middleware){
/*...*/
return function (context, next) {
let index = -1
return dispatch(0)
function dispatch(i) {
if (i <= index) return Promise.reject(new Error('next() called multiple times'))
index = i
const fn = middleware[i] || next
if (!fn) return Promise.resolve()
try {
return Promise.resolve(fn(context, function next() {
return dispatch(i + 1)
}))
} catch(err) {
return Promise.reject(err);
}
}
}
}
这个compose函数返回了一个有俩参数的函数(context和next),其中一个参数next在application.js的callback函数里是干啥用的呢?并没有使用到啊,百思不得其解。。。