/** * The AsyncEither datastructure represents an asynchronous operation that can * fail. At its heart it is implemented as `() => Promise>`. This * thunk makes it a performant but lazy operation at the expense of stack * safety. * * @module AsyncEither * @since 2.0.0 */ import "./_dnt.polyfills.js"; import type { Kind, Out } from "./kind.js"; import type { Applicable } from "./applicable.js"; import type { Async } from "./async.js"; import type { Bimappable } from "./bimappable.js"; import type { Combinable } from "./combinable.js"; import type { Either } from "./either.js"; import type { Failable } from "./failable.js"; import type { Flatmappable } from "./flatmappable.js"; import type { Initializable } from "./initializable.js"; import type { Mappable } from "./mappable.js"; import type { Wrappable } from "./wrappable.js"; /** * The AsyncEither type can best be thought of as an asynchronous function that * returns an `Either`. ie. `async () => Promise>`. This * forms the basis of most Promise based asynchronous communication in * TypeScript. * * @since 2.0.0 */ export type AsyncEither = Async>; /** * Specifies AsyncEither as a Higher Kinded Type, with covariant * parameter A corresponding to the 0th index of any substitutions and covariant * parameter B corresponding to the 1st index of any substitutions. * * @since 2.0.0 */ export interface KindAsyncEither extends Kind { readonly kind: AsyncEither, Out>; } /** * Constructs a AsyncEither from a value and wraps it in an inner *Left* * traditionally signaling a failure. * * @example * ```ts * import * as AE from "./async_either.ts"; * * const left = AE.left(1); * * const result = await left(); // Left(1); * ``` * * @since 2.0.0 */ export declare function left(left: B): AsyncEither; /** * Constructs a AsyncEither from a value and wraps it in an inner *Right* * traditionally signaling a successful computation. * * @example * ```ts * import * as AE from "./async_either.ts"; * * const right = AE.right(1); * * const result = await right(); // Right(1) * ``` * * @since 2.0.0 */ export declare function right(right: A): AsyncEither; /** * Wraps a Async of A in a try-catch block which upon failure returns B instead. * Upon success returns a *Right* and *Left* for a failure. * * @example * ```ts * import * as TE from "./async_either.ts"; * import * as E from "./either.ts"; * * const tryFetch = TE.tryCatch( * fetch, * (error, args) => ({ message: "Fetch Error", error, args }) * ); * * const result1 = await tryFetch("blah")(); // Left(ErrorStruct) * const result2 = await tryFetch("https://deno.land/")(); // Right(*Deno Website*) * ``` * * @since 2.0.0 */ export declare function tryCatch(fasr: (...as: AS) => A | PromiseLike, onThrow: (e: unknown, as: AS) => B): (...as: AS) => AsyncEither; /** * Lift an always succeeding async computation (Async) into a AsyncEither. * * @example * ```ts * import * as AE from "./async_either.ts"; * import * as A from "./async.ts"; * * const value = AE.fromAsync(A.wrap(1)); * * const result1 = await value(); // Right(1) * ``` * * @since 2.0.0 */ export declare function fromAsync(ta: Async): AsyncEither; /** * Lifts an Either into a AsyncEither. * * @example * ```ts * import * as AE from "./async_either.ts"; * import * as E from "./either.ts"; * * const value1 = AE.fromEither(E.right(1)); * const value2 = AE.fromEither(E.left("Error!")); * * const result1 = await value1(); // Right(1) * const result2 = await value2(); // Left("Error!") * ``` * * @since 2.0.0 */ export declare function fromEither(ta: Either): AsyncEither; /** * Construct an AsyncEither from a value A. * * @example * ```ts * import * as AE from "./async_either.ts"; * * const value = AE.wrap(1); * * const result = await value(); // Right(1) * ``` * * @since 2.0.0 */ export declare function wrap(a: A): AsyncEither; /** * Construct an AsyncEither from a value B. * * @example * ```ts * import * as AE from "./async_either.ts"; * * const value = AE.fail("Error!"); * * const result = await value(); // Left("Error!"); * ``` * * @since 2.0.0 */ export declare function fail(b: B): AsyncEither; /** * Map a function over the *Right* side of a AsyncEither * * @since 2.0.0 */ export declare function map(fai: (a: A) => I): (ta: AsyncEither) => AsyncEither; /** * Map a function over the *Left* side of a AsyncEither * * @since 2.0.0 */ export declare function mapSecond(fbj: (b: B) => J): (ta: AsyncEither) => AsyncEither; /** * Apply an argument to a function under the *Right* side. * * @since 2.0.0 */ export declare function apply(ua: AsyncEither): (ufai: AsyncEither I>) => AsyncEither; /** * Sequentially apply arguments * * @since 2.0.0 */ export declare function applySequential(ua: AsyncEither): (ufai: AsyncEither I>) => AsyncEither; /** * Chain AsyncEither based computations together in a pipeline * * ```ts * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; * import * as TE from "./async_either.ts"; * import * as E from "./either.ts"; * import { pipe } from "./fn.ts"; * * const ta = pipe( * TE.wrap(1), * TE.flatmap(n => TE.wrap(n*2)), * TE.flatmap(n => TE.wrap(n**2)) * ) * * assertEquals(await ta(), E.right(4)) * ``` * * @since 2.0.0 */ export declare function flatmap(fati: (a: A) => AsyncEither): (ta: AsyncEither) => AsyncEither; /** * @since 2.0.0 */ export declare function flatmapFirst(fati: (a: A) => AsyncEither): (ta: AsyncEither) => AsyncEither; /** * Chain AsyncEither based failures, *Left* sides, useful for recovering * from error conditions. * * ```ts * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; * import * as TE from "./async_either.ts"; * import * as E from "./either.ts"; * import { pipe } from "./fn.ts"; * * const ta = pipe( * TE.fail(1), * TE.recover(n => TE.wrap(n*2)), * TE.flatmap(n => TE.wrap(n**2)) * ) * * assertEquals(await ta(), E.right(4)) * ``` * * @since 2.0.0 */ export declare function recover(fbtj: (b: B) => AsyncEither): (ta: AsyncEither) => AsyncEither; /** * Provide an alternative for a failed computation. * Useful for implementing defaults. * * ```ts * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; * import * as TE from "./async_either.ts"; * import * as E from "./either.ts"; * import { pipe } from "./fn.ts"; * * const ta = pipe( * TE.fail(1), * TE.alt(TE.wrap(2)), * ) * * assertEquals(await ta(), E.right(2)) * ``` * * @since 2.0.0 */ export declare function alt(ti: AsyncEither): (ta: AsyncEither) => AsyncEither; /** * Fold away the inner Either from the `AsyncEither` leaving us with the * result of our computation in the form of a `Async` * * @since 2.0.0 */ export declare function match(onLeft: (left: L) => B, onRight: (right: R) => B): (ta: AsyncEither) => Async; /** * @since 2.0.0 */ export declare function getCombinableAsyncEither(CA: Combinable, CB: Combinable): Combinable>; /** * @since 2.0.0 */ export declare function getInitializableAsyncEither(CA: Initializable, CB: Initializable): Initializable>; /** * @since 2.0.0 */ export declare const ApplicableAsyncEither: Applicable; /** * @since 2.0.0 */ export declare const BimappableAsyncEither: Bimappable; /** * @since 2.0.0 */ export declare const FlatmappableAsyncEitherParallel: Flatmappable; /** * @since 2.0.0 */ export declare const FlatmappableAsyncEitherSequential: Flatmappable; /** * @since 2.0.0 */ export declare const FailableAsyncEitherParallel: Failable; /** * @since 2.0.0 */ export declare const FailableAsyncEitherSequential: Failable; /** * @since 2.0.0 */ export declare const MappableAsyncEither: Mappable; /** * @since 2.0.0 */ export declare const WrappableAsyncEither: Wrappable; /** * @since 2.0.0 */ export declare const tap: (fn: (value: A) => void) => (ua: AsyncEither) => AsyncEither; /** * @since 2.0.0 */ export declare const bind: (name: Exclude, faui: (a: A) => AsyncEither) => (ua: AsyncEither) => AsyncEither; /** * @since 2.0.0 */ export declare const bindTo: (name: N) => (ua: AsyncEither) => AsyncEither;