import { Maybe, MaybePatterns } from './Maybe.js'; import { EitherAsync } from './EitherAsync.js'; /** You can use this to extract the type of the `Just` value out of an `MaybeAsync`. */ export type ExtractJust = T extends PromiseLike> ? U : never; export interface MaybeAsyncTypeRef { /** Constructs a MaybeAsync object from a function that takes an object full of helpers that let you lift things into the MaybeAsync context and returns a Promise */ (runPromise: (helpers: MaybeAsyncHelpers) => PromiseLike): MaybeAsync; /** Constructs an MaybeAsync object from a function that returns a Maybe wrapped in a Promise */ fromPromise(f: () => Promise>): MaybeAsync; /** Constructs an MaybeAsync object from a Maybe */ liftMaybe(maybe: Maybe): MaybeAsync; /** Takes a list of `MaybeAsync`s and returns a Promise that will resolve with all `Just` values. Internally it uses `Promise.all` to wait for all results */ catMaybes(list: readonly MaybeAsync[]): Promise; } export interface MaybeAsync 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 MaybeAsync resolved to Nothing, * `run` will return a Promise resolved to Nothing. * If any of the promises were to be rejected then `run` will return * a Promise resolved to Nothing. * If an exception is thrown then `run` will return a Promise * resolved to Nothing. * If none of the above happen then a promise resolved to the * returned value wrapped in a Just will be returned. */ run(): Promise>; /** Transforms the value inside `this` with a given function. If the MaybeAsync that is being mapped resolves to Nothing then the mapping function won't be called and `run` will resolve the whole thing to Nothing, just like the regular Maybe#map */ map(f: (value: T) => U): MaybeAsync>; /** Transforms `this` with a function that returns a `MaybeAsync`. Behaviour is the same as the regular Maybe#chain */ chain(f: (value: T) => PromiseLike>): MaybeAsync; /** Converts `this` to a EitherAsync with a default error value */ toEitherAsync(error: L): EitherAsync; /** Runs an effect if `this` is `Just`, returns `this` to make chaining other methods possible */ ifJust(effect: (value: T) => any): MaybeAsync; /** Runs an effect if `this` is `Nothing`, returns `this` to make chaining other methods possible */ ifNothing(effect: () => any): MaybeAsync; /** Returns the default value if `this` is `Nothing`, otherwise it returns a Promise that will resolve to the value inside `this` */ orDefault(defaultValue: T): Promise; /** Maps the future value of `this` with another future `Maybe` function */ ap(maybeF: PromiseLike U>>): MaybeAsync>; /** Returns the first `Just` between the future value of `this` and another future `Maybe` or future `Nothing` if both `this` and the argument are `Nothing` */ alt(other: MaybeAsync): MaybeAsync; /** Returns `this` if it resolves to `Nothing`, otherwise it returns the result of applying the function argument to the value of `this` and wrapping it in a `Just` */ extend(f: (value: MaybeAsync) => U): MaybeAsync>; /** Takes a predicate function and returns `this` if the predicate, applied to the resolved value, is true or Nothing if it's false */ filter(pred: (value: T) => value is U): MaybeAsync; /** Takes a predicate function and returns `this` if the predicate, applied to the resolved value, is true or Nothing if it's false */ filter(pred: (value: T) => boolean): MaybeAsync; /** Flattens a `Maybe` nested inside a `MaybeAsync`. `m.join()` is equivalent to `m.chain(async x => x)` */ join(this: MaybeAsync>): MaybeAsync; /** Useful if you are not interested in the result of an operation */ void(): MaybeAsync; /** Structural pattern matching for `MaybeAsync` in the form of a function */ caseOf(patterns: MaybePatterns): Promise; finally(effect: () => any): MaybeAsync; 'fantasy-land/chain'(f: (value: T) => PromiseLike>): MaybeAsync; 'fantasy-land/alt'(other: MaybeAsync): MaybeAsync; 'fantasy-land/filter'(pred: (value: T) => value is U): MaybeAsync; 'fantasy-land/filter'(pred: (value: T) => boolean): MaybeAsync; /** WARNING: This is implemented only for Promise compatibility. Please use `chain` instead. */ then: PromiseLike>['then']; } export interface MaybeAsyncValue extends PromiseLike { } export interface MaybeAsyncHelpers { /** Allows you to take a regular Maybe value and lift it to the MaybeAsync context. Awaiting a lifted Maybe will give you the value inside. If the Maybe is Nothing then the function will exit immediately and MaybeAsync will resolve to Nothing after running it */ liftMaybe(maybe: Maybe): MaybeAsyncValue; /** Allows you to take a Maybe inside a Promise and lift it to the MaybeAsync context. Awaiting a lifted Promise will give you the value inside the Maybe. If the Maybe is Nothing or the Promise is rejected then the function will exit immediately and MaybeAsync will resolve to Nothing after running it */ fromPromise(promise: PromiseLike>): MaybeAsyncValue; } export declare const MaybeAsync: MaybeAsyncTypeRef;