UNPKG

@tunframework/tun

Version:

tun framework for node with typescript

22 lines (21 loc) 822 B
export function compose(composables) { return async (ctx, next) => { let index = -1; return (function dispatch(i) { if (i <= index) { return Promise.reject(new Error(`error: attempt to call composables[${i}], but current is composables[${index}], they migint called, or was given incorrect composables[${i}].`)); } index = i; let fn = i === composables.length ? next : composables[i]; if (!fn) return Promise.resolve(); try { // make `next` point to next composable return Promise.resolve(fn(ctx, dispatch.bind(null, i + 1))); } catch (e) { return Promise.reject(e); } })(0); }; }