compose
本文最后更新于:7 个月前
koa里的compose函数实现
function compose(middleList) {
return function fn(ctx, next) {
let index = -1;
return dispatch(0);
function dispatch(i) {
index = i;
let middle = middleList[i];
if (i === middleList.length) return next;
if(!middle) return Promise.resolve();
try {
return Promise.resolve(middle(ctx, dispatch.bind(null, i + 1)));
} catch (e) {
return Promise.reject(e);
}
}
};
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!