import { Either, EitherPatterns } from './Either.js'; import { MaybeAsync } from './MaybeAsync.js'; /** You can use this to extract the type of the `Right` value out of an `EitherAsync`. */ export type ExtractRight = T extends PromiseLike> ? R : never; /** You can use this to extract the type of the `Left` value out of an `EitherAsync`. */ export type ExtractLeft = T extends PromiseLike> ? L : never; export interface EitherAsyncTypeRef { /** Constructs an `EitherAsync` object from a function that takes an object full of helpers that let you lift things into the `EitherAsync` context and returns a Promise */ (runPromise: (helpers: EitherAsyncHelpers) => PromiseLike): EitherAsync; /** Constructs an `EitherAsync` object from a function that returns an Either wrapped in a Promise */ fromPromise(f: () => PromiseLike>): EitherAsync; /** Constructs an `EitherAsync` object from an Either */ liftEither(either: Either): EitherAsync; /** Takes a list of `EitherAsync`s and returns a Promise that will resolve with all `Left` values. Internally it uses `Promise.all` to wait for all results */ lefts(list: readonly EitherAsync[]): Promise; /** Takes a list of `EitherAsync`s and returns a Promise that will resolve with all `Right` values. Internally it uses `Promise.all` to wait for all results */ rights(list: readonly EitherAsync[]): Promise; /** Turns a list of `EitherAsync`s into an `EitherAsync` of list. The returned `Promise` will be rejected as soon as a single `EitherAsync` resolves to a `Left`, it will not wait for all Promises to resolve and since `EitherAsync` is lazy, unlike `Promise`, the remaining async operations will not be executed at all */ sequence(eas: readonly EitherAsync[]): EitherAsync; /** The same as `EitherAsync.sequence`, but it will run all async operations at the same time rather than sequentially */ all(eas: readonly EitherAsync[]): EitherAsync; } export interface EitherAsync extends PromiseLike> { /** * It's important to remember how `run` will behave because in an * async context there are other ways for a function to fail other * than to return a Nothing, for example: * If any of the computations inside EitherAsync resolved to a Left, * `run` will return a Promise resolved to that Left. * If any of the promises were to be rejected then `run` will return * a Promise resolved to a Left with the rejection value inside * If an exception is thrown then `run` will return a Promise * resolved to a Left with the exception inside * If none of the above happen then a promise resolved to the * returned value wrapped in a Right will be returned */ run(): Promise>; /** Given two functions, maps the value that the Promise inside `this` resolves to using the first if it is `Left` or using the second one if it is `Right` */ bimap(f: (value: L) => L2, g: (value: R) => R2): EitherAsync, Awaited>; /** Transforms the `Right` value of `this` with a given function. If the `EitherAsync` that is being mapped resolves to a Left then the mapping function won't be called and `run` will resolve the whole thing to that Left, just like the regular Either#map */ map(f: (value: R) => R2): EitherAsync>; /** Maps the `Left` value of `this`, acts like an identity if `this` is `Right` */ mapLeft(f: (value: L) => L2): EitherAsync, R>; /** Transforms `this` with a function that returns a `EitherAsync`. Behaviour is the same as the regular Either#chain */ chain(f: (value: R) => PromiseLike>): EitherAsync; /** The same as EitherAsync#chain but executes the transformation function only if the value is Left. Useful for recovering from errors */ chainLeft(f: (value: L) => PromiseLike>): EitherAsync; /** Flattens an `Either` nested inside an `EitherAsync`. `e.join()` is equivalent to `e.chain(async x => x)` */ join(this: EitherAsync>): EitherAsync; /** Converts `this` to a MaybeAsync, discarding any error values */ toMaybeAsync(): MaybeAsync; /** Returns `Right` if `this` is `Left` and vice versa */ swap(): EitherAsync; /** Runs an effect if `this` is `Left`, returns `this` to make chaining other methods possible */ ifLeft(effect: (value: L) => any): EitherAsync; /** Runs an effect if `this` is `Right`, returns `this` to make chaining other methods possible */ ifRight(effect: (value: R) => any): EitherAsync; /** Applies a `Right` function wrapped in `EitherAsync` over a future `Right` value. Returns `Left` if either the `this` resolves to a `Left` or the function is `Left` */ ap(other: PromiseLike R2>>): EitherAsync>; /** Returns the first `Right` between the future value of `this` and another `EitherAsync` or the `Left` in the argument if both `this` and the argument resolve to `Left` */ alt(other: EitherAsync): EitherAsync; /** Returns `this` if it resolves to a `Left`, otherwise it returns the result of applying the function argument to `this` and wrapping it in a `Right` */ extend(f: (value: EitherAsync) => R2): EitherAsync>; /** Returns a Promise that resolves to the value inside `this` if it\'s `Left` or a default value if `this` is `Right` */ leftOrDefault(defaultValue: L): Promise; /** Returns a Promise that resolves to the value inside `this` if it\'s `Right` or a default value if `this` is `Left` */ orDefault(defaultValue: R): Promise; /** Useful if you are not interested in the result of an operation */ void(): EitherAsync; /** Structural pattern matching for `EitherAsync` in the form of a function */ caseOf(patterns: EitherPatterns): Promise; finally(effect: () => any): EitherAsync; 'fantasy-land/chain'(f: (value: R) => PromiseLike>): EitherAsync; 'fantasy-land/alt'(other: EitherAsync): EitherAsync; /** WARNING: This is implemented only for Promise compatibility. Please use `chain` instead. */ then: PromiseLike>['then']; } export interface EitherAsyncValue extends PromiseLike { } export interface EitherAsyncHelpers { /** Allows you to take a regular Either value and lift it to the `EitherAsync` context. Awaiting a lifted Either will give you the `Right` value inside. If the Either is Left then the function will exit immediately and EitherAsync will resolve to that Left after running it */ liftEither(either: Either): EitherAsyncValue; /** Allows you to take an Either inside a Promise and lift it to the `EitherAsync` context. Awaiting a lifted Promise will give you the `Right` value inside the Either. If the Either is Left or the Promise is rejected then the function will exit immediately and MaybeAsync will resolve to that Left or the rejection value after running it */ fromPromise(promise: PromiseLike>): EitherAsyncValue; /** A type safe version of throwing an exception. Unlike the Error constructor, which will take anything, throwE only accepts values of the same type as the Left part of the Either */ throwE(error: L): never; } export declare const EitherAsync: EitherAsyncTypeRef;