return(ctx, next) => { return dispatch(0) functiondispatch(i) { if (i <= index) returnPromise.reject(newError('next() called multile times')) index = i let fn = middleware[i] if (i === middleware.length) fn = next if (!fn) returnPromise.resolve() try { returnPromise.resolve(fn(ctx, dispatch.bind(null, i + 1))) } catch (err) { returnPromise.reject(err) } } } }