functioncompose (middleware) { if (!Array.isArray(middleware)) thrownewTypeError('Middleware stack must be an array!') for (const fn of middleware) { if (typeof fn !== 'function') thrownewTypeError('Middleware must be composed of functions!') }
returnfunction (context, next) { // last called middleware # let index = -1 return dispatch(0) functiondispatch (i) { if (i <= index) returnPromise.reject(newError('next() called multiple times')) index = i let fn = middleware[i] if (i === middleware.length) fn = next if (!fn) returnPromise.resolve() try { returnPromise.resolve(fn(context, functionnext () { return dispatch(i + 1) })) } catch (err) { returnPromise.reject(err) } } } }