简化版
function compose(middleware) {
return function() {
function dispatch(i) {
let fn = middleware[i]
if (!fn) return
return fn(function next()) {
return dispatch(i + 1)
}
}
return dispatch(0)
}
}
Promise 版
function compose(middleware) {
return function(ctx) {
return dispatch(0)
function dispatch(i) {
let fn = middleware[i]
if (!fn) return Promise.resolve()
return Promise.resolve(
fn(function next() {
return dispatch(i + 1)
})
)
}
}
}