import { pipe } from 'fp-ts/lib/function'; import * as Either from 'fp-ts/lib/Either'; import { TaskEither } from 'fp-ts/lib/TaskEither'; /** * Given a taskEither, execute the task, await the result and return a promise. * It is used a lot as nest rely pretty heavily on Promise * * @param task: a TaskEither */ export const executeTask = async (task: TaskEither): Promise => { const result = await task(); return new Promise((resolve, reject) => { pipe(result, Either.fold(reject, resolve)); }); }; /** * Given a taskEither, execute the task, and call the onLeft or OnRight callback. * * @param task: a TaskEither * @param onLeft * @param onRight */ export const executeAndHandle = async (task: TaskEither, onLeft: (error: E) => B, onRight: (data: A) => B): Promise => { const result = await task(); return pipe(result, Either.fold(onLeft, onRight)); };