getOrElse<A, B> function

Future<B> Function(TaskEither<A, B>) getOrElse<A, B>(
  1. B defaultValue(
    1. A
    )
)

Retrieves the value from a TaskEither<A, B>, or produces a default value if it's a e.Left.

Usage:

final taskEither = TaskEither.value(e.Right<int, int>(5));
final result = await getOrElse<int, int>((error) => -1)(taskEither);

// `result` will be 5 if `taskEither` contains a Right(5), or -1 if it contains a Left.

Implementation

Future<B> Function(TaskEither<A, B>) getOrElse<A, B>(
    B Function(A) defaultValue) {
  return (TaskEither<A, B> taskEither) async {
    final result = await taskEither.value();
    return result is e.Left<A, B>
        ? defaultValue(result.value)
        : (result as e.Right<A, B>).value;
  };
}