Last active
September 27, 2021 06:21
-
-
Save nuintun/df00cdadeb190a6e9bd72cfa380c6f99 to your computer and use it in GitHub Desktop.
koa-compose 简化版实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @function compose | |
* @description compose koa middlewares | |
* @param {function[]} middlewares | |
* @returns {function} | |
*/ | |
function compose(middlewares) { | |
const done = async () => {}; | |
const compose = middlewares.reduce((compose, middleware) => { | |
return (context, next) => { | |
return compose(context, async () => { | |
middleware(context, next); | |
}); | |
}; | |
}); | |
return async (context, next = done) => { | |
return compose ? compose(context, next) : next(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS:官方递归实现版更优,可以按需初始化!