INNER CODE UNIT · TypeScript

flatten

grammyjs/grammY · src/composer.ts:138

function flatten<C extends Context>(mw: Middleware<C>): MiddlewareFn<C> {
    return typeof mw === "function"
        ? mw
        : (ctx, next) => mw.middleware()(ctx, next);
}
function concat<C extends Context>(
    first: MiddlewareFn<C>,
    andThen: MiddlewareFn<C>,
): MiddlewareFn<C> {
    return async (ctx, next) => {
        let nextCalled = false;
        await first(ctx, async () => {
            if (nextCalled) throw new Error("`next` already called before!");
            else nextCalled = true;
            await andThen(ctx, next);
        });
    };
}

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…