将业务流程代码中间件化
class MidWare{ constructor(){ this.cache = []; } use(fn){ this.cache.push(fn); } start(){ var middles = this.cache; function dispatch(index){ if(index==middles.length){ return new Promise.resolve(); } return new Promise((resolve,reject)=>{ resolve(middles[index](()=>dispatch(index+1))); }) } return dispatch(0); } } var m = new MidWare(); m.use(function(next){ console.log(1); next(); }) m.use(function(next){ console.log(2); next(); }) m.use(function(next){ console.log(3); next(); }) m.start()