You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
consthttp=require('http')functioncompose(middlewareList){returnfunction(ctx){functionnext(i){constfn=middlewareList[i]if(!fn){returnPromise.resolve()}try{returnPromise.resolve(fn(ctx,next.bind(null,i+1)))}catch(err){Promise.reject(err)}}returnnext(0)}}classApp{constructor(){this.middlewares=[]}use(fn){this.middlewares.push(fn)returnthis}callback(){constfn=compose(this.middlewares)return(req,res)=>{constctx={ req,res }consthandleResponse=()=>{console.log('handle response')}returnfn(ctx).then(handleResponse)}}listen(...args){constserver=http.createServer(this.callback())returnserver.listen(...args)}}asyncfunctionoutput1(ctx,next){console.log('start 1')awaitnewPromise((resolve)=>{setTimeout(()=>{resolve()},1000)})awaitnext()console.log('end 1')}asyncfunctionoutput2(ctx,next){console.log('start 2')awaitnewPromise((resolve)=>{setTimeout(async()=>{resolve()},2000)})awaitnext()console.log('end 2')}asyncfunctionoutput3(ctx,next){console.log('start 3')awaitnext()console.log('end 3')}constapp=newApp()app.use(output1)app.use(output2)app.use(output3)app.listen(3000,()=>{console.log('server is running on port 3000')})
express - 串联模型
koa - 洋葱模型(加入promsie + async/await 异步机制即可)
compose
The text was updated successfully, but these errors were encountered: