• English
  • prefer-result-async

    Enforces use of ResultAsync<T, E> instead of Promise<Result<T, E>>.

    Rule details

    ResultAsync<T, E> is an alias for Promise<Result<T, E>>. Using the dedicated type alias is shorter, more expressive, and signals clearly that the function returns a Result-wrapped async value. This rule auto-fixes violations.

    Incorrect

    import { 
    type Result = Promise<Result.Result<string, Error>>
    Result
    } from '@praha/byethrow';
    // ❌ using Promise<Result<...>> type
    type Result = Promise<Result.Result<string, Error>>
    Result
    =
    interface Promise<T>

    Represents the completion of an asynchronous operation

    Promise
    <
    import Result
    Result
    .
    type Result<T, E> = Result.Success<T> | Result.Failure<E>

    A union type representing either a success or a failure.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const doSomething = (): Result.Result<number, string> => {
      return Math.random() > 0.5
        ? { type: 'Success', value: 10 }
        : { type: 'Failure', error: 'Oops' };
    };
    @categoryCore Types
    Result
    <string, Error>>;

    Correct

    import { 
    type Result = Promise<Result.Result<string, Error>>
    Result
    } from '@praha/byethrow';
    // ✅ using ResultAsync type
    type Result = Promise<Result.Result<string, Error>>
    Result
    =
    import Result
    Result
    .
    type ResultAsync<T, E> = Promise<Result.Result<T, E>>

    An asynchronous variant of Result , wrapped in a Promise.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const fetchData = async (): Result.ResultAsync<string, Error> => {
      try {
        const data = await fetch('...');
        return { type: 'Success', value: await data.text() };
      } catch (err) {
        return { type: 'Failure', error: err as Error };
      }
    };
    @categoryCore Types
    ResultAsync
    <string, Error>;