import {Right, Either, ILeft, IRight, either, Left} from './either'; import {head, tail, isEmpty, } from "./array"; import {pipe} from "./pipe"; type AsyncFn = (input: T) => Promise>; const recurse = (functions: any[]) => (res: T) => pipeEither(res, ...functions as [any]); export async function pipeEither(input: T): Promise>; export async function pipeEither(input: T, f1: AsyncFn): Promise>; export async function pipeEither(input: T, f1: AsyncFn, f2: AsyncFn): Promise | ILeft>; export async function pipeEither(input: T, f1: AsyncFn, f2: AsyncFn, f3: AsyncFn): Promise | ILeft | ILeft>; export async function pipeEither(input: T, f1: AsyncFn, f2: AsyncFn, f3: AsyncFn, f4: AsyncFn): Promise | ILeft | ILeft | ILeft>; export async function pipeEither(input: any, ...functions: Array<(input: unknown) => Promise>>): Promise { return isEmpty(functions) ? Right(input) : either( Left, pipe(functions, tail, recurse), await head(functions)(input) ) }