js洋葱模型,支持异步async

const sleep=require('./utils/sleep')
//js洋葱模型,支持异步async
function compose (middleware) {
    return async function () {
        let args = arguments
        await dispatch(0);
        async function dispatch (i) {
            const fn = middleware[i]
            if (!fn) return null
            await fn( ...args,async function next () {
                await dispatch(i + 1)
            })
        }
    }
}
//demo
let middleware = []
middleware.push(async (next) => {
    console.log(0)
    await next()
    console.log(3)
})
middleware.push(async (next) => {
    console.log(1)
    await sleep(2000)
    await next()
    console.log(1.1)
})
middleware.push(() => {
    console.log(2)
})

const func=compose(middleware)
func()

 

posted @ 2019-08-02 15:26  无工时代  阅读(407)  评论(0编辑  收藏  举报