UNPKG

@tunframework/tun

Version:

tun framework for node with typescript

26 lines (25 loc) 949 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compose = void 0; 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); }; } exports.compose = compose;