zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    java学习的第三天
    java学习的第二天
    java学习的第一天
    兼容性测试2
    兼容性测试
    安全性测试
    界面测试
    功能性测试
    简历小技巧
    day13
  • 原文地址:https://www.cnblogs.com/caoke/p/11288790.html
Copyright © 2011-2022 走看看