import { Result, type ResultRecord } from './result.js'; import { Unit } from './unit.js'; import { Action, ActionOfT, AsyncAction, AsyncActionOfT, AsyncFunctionOfT, AsyncFunctionOfTtoK, FunctionOfT, FunctionOfTtoK, PredicateOfT, ResultMatcher, ResultMatcherNoReturn, Some } from './utilities.js'; /** * Represents and asynchronous Result that could succeed with a value or fail with an error */ export declare class ResultAsync { /** * Combines several results (and any error messages) into a single result. * The returned result will be a failure if any of the input results are failures. * * @param results The Results to be combined. * @returns A Result that is a success when all the input results are also successes. */ static combine | ResultAsync>>(results: TResultRecord): ResultAsync>; /** * Combines several results (and any error messages) into a single result. * The returned result will be a failure if any of the input results are failures. * * Asynchronous operations are executed one after another. * * @param results The Results to be combined. * @returns A Result that is a success when all the input results are also successes. */ static combineInOrder | ResultAsync>>(results: TResultRecord): ResultAsync>; /** * Creates a new ResultAsync from the given Result * @param value a successful or failed Result * @returns */ static from(value: Result): ResultAsync; /** * Creates a new ResultAsync from the given Promise * @param value a Promise resolving to a Result */ static from(value: Promise>): ResultAsync; /** * Creates a new ResultAsync from the given Promise * @param value a Promise which will be converted into a successful Result if it resolves * and a failed Result if it rejects */ static from(value: Promise>): ResultAsync; /** * Creates a new successful ResultAsync with the inner value * of the evaluated async function. If the Promise rejects, a failed ResultAsync will * be returned with an error created by the provided errorHandler * @param promise * @param errorHandler */ static try(func: AsyncFunctionOfT>, errorHandler: FunctionOfTtoK> | AsyncFunctionOfTtoK>): ResultAsync; static try(func: AsyncAction, errorHandler: FunctionOfTtoK> | AsyncFunctionOfTtoK>): ResultAsync; /** * Creates a new successful ResultAsync with the inner value * of the given Promise. If the Promise rejects, a failed ResultAsync will * be returned with an error created by the provided errorHandler * @param promise * @param errorHandler */ static try(promise: Promise>, errorHandler: FunctionOfTtoK> | AsyncFunctionOfTtoK>): ResultAsync; /** * Creates a new successful ResultAsync with a Unit value. * If the Promise rejects, a failed ResultAsync will * be returned with an error created by the provided errorHandler * @param promise * @param errorHandler */ static try(promise: Promise, errorHandler: FunctionOfTtoK> | AsyncFunctionOfTtoK>): ResultAsync; /** * Creates a new successful ResultAsync with a Unit value */ static success(): ResultAsync; /** * Creates a new successful ResultAsync with the given value * @param value */ static success(value: Some): ResultAsync; /** * Creates a new failed ResultAsync with the given error * @param error */ static failure(error: Some): ResultAsync; private value; protected constructor(value: Promise>); /** * True if the Result was successful */ get isSuccess(): Promise; /** * True if the Result failed */ get isFailure(): Promise; /** * Will return the inner value created from executing the Result * if it was successful, otherwise it will throw an Error * @returns the result of a successful Result */ getValueOrThrow(): Promise>; /** * Gets the value of a successful ResultAsync, and a default value if the ResultAsync failed * @param defaultValue returned if the ResultAsync was not successful */ getValueOrDefault(defaultValue: Some): Promise>; /** * Gets the value of a successful ResultAsync, and a default value if the ResultAsync failed * @param valueFactory executed and returned if the ResultAsync was not successful */ getValueOrDefault(valueFactory: FunctionOfT>): Promise>; /** * Will return the inner error value of the Result * if it failed, otherwise it will throw an Error * @returns the error of a failed Result */ getErrorOrThrow(): Promise>; /** * Gets the error of a failed ResultAsync, and a default value if the ResultAsync was successful * @param defaultError returned if the Result was successful */ getErrorOrDefault(defaultError: Some): Promise>; /** * Gets the error of a failed ResultAsync, and a default value if the ResultAsync was successful * @param errorCreator executed and returned if the Result was successful */ getErrorOrDefault(errorCreator: FunctionOfT>): Promise>; /** * Checks the value of a given predicate against the Result's inner value, * if the Result already succeeded * @param predicate check against the Result's inner value * @param errorOrErrorCreator either an error value or a function to create an error from the Result's inner value * @returns a successful ResultAsync if the predicate is true, and a failed one if not */ ensure(predicate: PredicateOfT, errorOrErrorCreator: Some | FunctionOfTtoK> | AsyncFunctionOfTtoK>): ResultAsync; pipe(): ResultAsync; pipe(op1: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn, op3: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn, op3: ResultAsyncOpFn, op4: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn, op3: ResultAsyncOpFn, op4: ResultAsyncOpFn, op5: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn, op3: ResultAsyncOpFn, op4: ResultAsyncOpFn, op5: ResultAsyncOpFn, op6: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn, op3: ResultAsyncOpFn, op4: ResultAsyncOpFn, op5: ResultAsyncOpFn, op6: ResultAsyncOpFn, op7: ResultAsyncOpFn): ResultAsync; pipe(op1: ResultAsyncOpFn, op2: ResultAsyncOpFn, op3: ResultAsyncOpFn, op4: ResultAsyncOpFn, op5: ResultAsyncOpFn, op6: ResultAsyncOpFn, op7: ResultAsyncOpFn, op8: ResultAsyncOpFn): ResultAsync; /** * Converts the value of a successful ResultAsync to a new value. * @param projection an async function which accepts the current value as a parameter and returns a new value */ map(projection: AsyncFunctionOfTtoK>): ResultAsync; /** * Converts the value of a successful ResultAsync to a new value. * @param projection function which accepts the current value as a parameter and returns a new value */ map(projection: FunctionOfTtoK>): ResultAsync; /** * Converts the error of a failed ResultAsync to a new error * @param projection an async function given the error of a failed ResultAsync which returns a new error */ mapError(projection: AsyncFunctionOfTtoK>): ResultAsync; /** * Converts the error of a failed ResultAsync to a new error * @param projection a function given the error of a failed ResultAsync which returns a new error */ mapError(projection: FunctionOfTtoK>): ResultAsync; /** * Converts a failed Result into a successful one * @param projection an async function that maps the error of the current ResultAsync to a value */ mapFailure(projection: AsyncFunctionOfTtoK>): ResultAsync; /** * Converts a failed Result into a successful one * @param projection a function that maps the error of the current ResultAsync to a value */ mapFailure(projection: FunctionOfTtoK>): ResultAsync; /** * Maps the successful ResultAsync to a new Result, which is wrapped in a ResultAsync * @param projection a function given the value of the successful ResultAsync which returns a new Result */ bind(projection: FunctionOfTtoK>): ResultAsync; /** * Maps the successful ResultAsync to a new ResultAsync * @param projection a function given the value of the successful ResultAsync which returns a new ResultAsync */ bind(projection: FunctionOfTtoK>): ResultAsync; /** * Maps a failed ResultAsync to a new ResultAsync * @deprecated Please use `compensate` instead * @param projection * @returns */ bindFailure(projection: FunctionOfTtoK | ResultAsync>): ResultAsync; /** * Maps a failed ResultAsync to a new ResultAsync * @param projection * @returns */ compensate(projection: FunctionOfTtoK | ResultAsync>): ResultAsync; /** * Executes the given async action if the ResultAsync is successful * @param action an async function given the value of the successful ResultAsync */ tap(asyncAction: AsyncActionOfT): ResultAsync; /** * Executes the given action if the ResultAsync is successful * @param action a function given the value of the successful ResultAsync */ tap(action: ActionOfT): ResultAsync; /** * Executes the action if the condition is true and the ResultAsync is successful * @param condition a boolean value * @param action a function given the inner value of the successful ResultAsync * @returns */ tapIf(condition: boolean, action: ActionOfT): ResultAsync; /** * Executes the action if the predicate is true and the ResultAsync is successful * @param predicate a function given the inner value of the successful ResultAsync which returns a boolean value * @param action a function given the inner value of the successful ResultAsync * @returns */ tapIf(predicate: PredicateOfT, action: ActionOfT): ResultAsync; /** * Executes the given async action if the ResultAsync is successful or failed * @param action an async function * @returns the current ResultAsync wrapping the inner Result */ tapEither(asyncAction: AsyncAction): ResultAsync; /** * Executes the given action if the ResultAsync is successful or failed * @param action a function * @returns the current ResultAsync wrapping the inner Result */ tapEither(action: Action): ResultAsync; /** * * @param matcher */ match(matcher: ResultMatcher): Promise>; match(matcher: ResultMatcherNoReturn): Promise; /** * Maps both failed and successful ResultAsync to a new value * @param projection a function given the inner Result of the ResultAsync that returns a new value * @returns */ finally(projection: FunctionOfTtoK, Some>): Promise>; /** * Executes the given action when the ResultAsync is failed * @param action a function given the inner error of the failed ResultAsync */ tapFailure(action: AsyncActionOfT): ResultAsync; /** * Executes the given async action with the ResultAsync is failed * @param action an async function given the inner error of the failed ResultAsync */ tapFailure(action: ActionOfT): ResultAsync; /** * Attemps the given async action if the ResultAsync is successful, converting any rejected Promises into a failed ResultAsync * @param action an async function given the inner value of a successful ResultAsync * @param errorHandler a function that converts the value of a rejected Promise into an error for the failed ResultAsync */ onSuccessTry(action: AsyncActionOfT, errorHandler: FunctionOfTtoK>): ResultAsync; /** * Attempts the given action if the ResultAsync is successful, converting any thrown errors into a failed ResultAsync * @param action a function given the inner value of a successful ResultAsync * @param errorHandler a function that converts an error thrown by the action into an error for the failed ResultAsync */ onSuccessTry(action: ActionOfT, errorHandler: FunctionOfTtoK>): ResultAsync; /** * Returns the inner Promise, wrapping a failed Result for a rejected Promise with the * given errorHandler if provided, othewise rejected Promise handling * is left to the caller. * @param errorHandler a value or Promise returning error handler that converts a rejected Promise * to a failed Result. * @returns */ toPromise(errorHandler?: FunctionOfTtoK> | AsyncFunctionOfTtoK>): Promise>; } export type ResultAsyncOpFn = FunctionOfTtoK, ResultAsync>;