asyncMap<T> method

Future<Result<T>> asyncMap<T>(
  1. FutureOr<T> transform(
    1. S value
    )
)

Transforms the successful value inside the Result asynchronously.

If the current Result is a success, the transform function is applied to its value, wrapping the new value in a SuccessResult. If the current Result is already a failure, the transform step is skipped, and the failure is forwarded downstream.

Example:

Future<Result<int>> idResult = fetchUser().asyncMap((user) => user.id);

Implementation

Future<Result<T>> asyncMap<T>(FutureOr<T> Function(S value) transform) async {
  final result = await this;

  return switch (result) {
    SuccessResult<S>(success: Success(:final value)) =>
      Result.success(await transform(value)),
    FailureResult<S>(failure: final failure) => Result.failure(failure),
  };
}