• 日本語
  • prefer-result-async

    Promise<Result<T, E>> の代わりに ResultAsync<T, E> の使用を強制します。

    ルールの詳細

    ResultAsync<T, E>Promise<Result<T, E>> のエイリアスです。専用のエイリアスはより短く、関数が Result でラップされた非同期値を返すことを明確に示します。このルールは自動修正に対応しています。

    誤り

    import { 
    type Result = Promise<Result.Result<string, Error>>
    Result
    } from '@praha/byethrow';
    // ❌ 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>>;

    正しい

    import { 
    type Result = Promise<Result.Result<string, Error>>
    Result
    } from '@praha/byethrow';
    // ✅ 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>;