Results: {
    combine<T extends unknown[]>(
        ...results: {
            [K in string | number | symbol]: Result<T[K<K>], DomainError>
        },
    ): Result<T, DomainError>;
    all<T>(results: Result<T, DomainError>[]): Result<T[], DomainError>;
    sequence<T>(
        operations: () => Promise<Result<T, DomainError>>[],
    ): Promise<Result<T[], DomainError>>;
    parallel<T>(
        operations: () => Promise<Result<T, DomainError>>[],
    ): Promise<Result<T[], DomainError>>;
    retry<T>(
        operation: () => Promise<Result<T, DomainError>>,
        options?: { maxRetries?: number; delay?: number },
    ): Promise<Result<T, DomainError>>;
    toOptional<T>(result: Result<T, DomainError>): null | T;
    unwrapOrThrow<T>(result: Result<T, DomainError>): T;
} = ...

Advanced result helpers for common patterns.

These helpers simplify working with Results, especially in command handlers and complex business logic flows.

Type declaration

  • combine:function
    • Combines multiple Results into one. Returns Failure if any Result is Failure, otherwise Success with tuple of values.

      Type Parameters

      • T extends unknown[]

      Parameters

      • ...results: { [K in string | number | symbol]: Result<T[K<K>], DomainError> }

        Results to combine

      Returns Result<T, DomainError>

      Combined Result with tuple of values

      const result = Results.combine(
      ProductName.create('Laptop'),
      Money.create(999, 'USD')
      );
      // result is Success<[ProductName, Money]> or Failure<DomainError>
  • all:function
    • Maps over an array of Results, collecting successes or returning first failure.

      Type Parameters

      • T

      Parameters

      Returns Result<T[], DomainError>

      Result with array of values or first failure

      const emailResults = emails.map(e => Email.create(e));
      const validEmails = Results.all(emailResults);
      // validEmails is Success<Email[]> or Failure<DomainError>
  • sequence:function
    • Executes async operations sequentially, stopping on first failure.

      Type Parameters

      • T

      Parameters

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

      Result with array of values or first failure

      const result = await Results.sequence([
      () => validateUser(userId),
      () => checkPermissions(userId),
      () => performAction()
      ]);
  • parallel:function
    • Executes async operations in parallel, collecting all failures.

      Type Parameters

      • T

      Parameters

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

      Result with array of values or aggregated failure

      const result = await Results.parallel([
      () => fetchUser(id1),
      () => fetchUser(id2),
      () => fetchUser(id3)
      ]);
  • retry:function
    • Retries an operation with exponential backoff.

      Type Parameters

      • T

      Parameters

      • operation: () => Promise<Result<T, DomainError>>

        The operation to retry

      • options: { maxRetries?: number; delay?: number } = {}

        Retry options (maxRetries, delay)

      Returns Promise<Result<T, DomainError>>

      Result of the operation after retries

      const result = await Results.retry(
      () => callExternalAPI(),
      { maxRetries: 3, delay: 100 }
      );
  • toOptional:function
  • unwrapOrThrow:function
    • Throws if Failure, returns value if Success. Use with caution - prefer explicit error handling.

      Type Parameters

      • T

      Parameters

      Returns T

      The success value

      The error if Failure

      const value = Results.unwrapOrThrow(result); // throws if failure
      
// Combine multiple Results
const nameResult = ProductName.create(command.name);
const priceResult = Money.create(command.price);

return Results.combine(nameResult, priceResult)
.map(([name, price]) => Product.create(name, price));