let middleware = [] middleware.push((next) => { console.log(1) next() console.log(1.1) }) middleware.push((next) => { console.log(2) next() console.log(2.1) }) middleware.push((next) => { console.log(3) next() console.log(3.1) }) let fn = compose(middleware) fn() // /* // 1 // 2 // 3 // 3.1 // 2.1 // 1.1 // */ function compose (middleware) { return function() { dispatch(0) function dispatch(i) { const fn = middleware[i] if(!fn) return null fn(function next() { dispatch(i+1) }) } } }