Variable AsyncResultsConst

AsyncResults: {
    fromPromise<T>(
        promise: Promise<T>,
        errorMapper?: (error: unknown) => DomainError,
    ): Promise<Result<T, DomainError>>;
    map<T, U>(
        result: Promise<Result<T, DomainError>>,
        fn: (value: T) => U | Promise<U>,
    ): Promise<Result<U, DomainError>>;
    flatMap<T, U>(
        result: Promise<Result<T, DomainError>>,
        fn: (value: T) => Promise<Result<U, DomainError>>,
    ): Promise<Result<U, DomainError>>;
    sequence<T>(
        operations: () => Promise<Result<T, DomainError>>[],
    ): Promise<Result<T[], DomainError>>;
    parallel<T>(
        operations: () => Promise<Result<T, DomainError>>[],
    ): Promise<Result<T[], DomainError>>;
} = ...

Helpers for working with async operations that return Results.

These helpers simplify common patterns when dealing with Promises and Results, reducing boilerplate code and improving error handling.

Type declaration

  • fromPromise:function
    • Wraps a Promise that might throw into a Result.

      Catches any errors from the promise and converts them to Failure with DomainError.

      Type Parameters

      • T

      Parameters

      • promise: Promise<T>

        The promise to wrap

      • OptionalerrorMapper: (error: unknown) => DomainError

        Optional function to map error to DomainError

      Returns Promise<Result<T, DomainError>>

      Promise resolving to Success or Failure

      const result = await AsyncResults.fromPromise(
      repository.findById(id),
      (error) => new DomainError('DB_ERROR', `Database error: ${error}`)
      );

      if (result.isSuccess) {
      console.log('Found:', result.value);
      }
  • map:function
    • Maps over an async Result, transforming the success value.

      If the Result is a Failure, it's returned as-is. The mapping function can be sync or async.

      Type Parameters

      • T
      • U

      Parameters

      • result: Promise<Result<T, DomainError>>

        Promise of Result to map over

      • fn: (value: T) => U | Promise<U>

        Mapping function (sync or async)

      Returns Promise<Result<U, DomainError>>

      Promise of mapped Result

      const userResult = await getUserById(id);
      const nameResult = await AsyncResults.map(
      userResult,
      (user) => user.name.toUpperCase()
      );
  • flatMap:function
    • Flat maps over an async Result, chaining async operations that return Results.

      If the Result is a Failure, it's returned as-is without calling the function. Useful for chaining async operations that can fail.

      Type Parameters

      • T
      • U

      Parameters

      Returns Promise<Result<U, DomainError>>

      Promise of final Result

      const result = await AsyncResults.flatMap(
      AsyncResults.fromPromise(getUserId()),
      async (userId) => {
      const user = await repository.findById(userId);
      return user ? Success.create(user) : Failure.create(notFoundError);
      }
      );
  • sequence:function
    • Executes multiple async Results in sequence, collecting all results.

      Similar to Results.sequence but for async operations. Short-circuits on first failure.

      Type Parameters

      • T

      Parameters

      Returns Promise<Result<T[], DomainError>>

      Promise of Result with array of all success values

      const results = await AsyncResults.sequence([
      () => saveUser(user1),
      () => saveUser(user2),
      () => saveUser(user3)
      ]);

      if (results.isSuccess) {
      console.log('All saved:', results.value);
      }
  • parallel:function
    • Executes multiple async Results in parallel, collecting all results.

      Similar to Results.parallel but waits for all promises. Returns first encountered failure, or success with all values.

      Type Parameters

      • T

      Parameters

      Returns Promise<Result<T[], DomainError>>

      Promise of Result with array of all success values

      const results = await AsyncResults.parallel([
      () => fetchUser(id1),
      () => fetchUser(id2),
      () => fetchUser(id3)
      ]);

      if (results.isSuccess) {
      console.log('All fetched:', results.value);
      }
// Wrap async operation that might throw
const result = await AsyncResults.fromPromise(
fetch('/api/users'),
(error) => new DomainError('FETCH_ERROR', String(error))
);

// Chain async operations
const finalResult = await AsyncResults.flatMap(
AsyncResults.fromPromise(getUserId()),
async (userId) => getUserData(userId)
);