Skip to content

Add a type for TaskEither<L, Option<T>> #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
evandrobubiak opened this issue May 31, 2025 · 0 comments
Open

Add a type for TaskEither<L, Option<T>> #164

evandrobubiak opened this issue May 31, 2025 · 0 comments

Comments

@evandrobubiak
Copy link

I have been used much a typedef like this and added some util extension.
I use this for API Calls that may return something or not and may fail (bad connection, timout, etc).
Is there something out of the box for this? If not it could be an feature in fpdart.

typedef TaskResultOption<T> = TaskEither<BaseAppException, Option<T>>;

extension TaskResultOptionExtension<T> on TaskResultOption<T> {
  TaskEither<BaseAppException, T> toSomeOrLeft() {
    return flatMap(
      (option) => option.fold(
        () => TaskEither.left(DataNotFoundException()),
        (value) => TaskEither.right(value),
      ),
    );
  }

  TaskEither<BaseAppException, T?> toSomeOrNull() {
    return flatMap(
      (option) => option.fold(
        () => TaskEither.right(null),
        (value) => TaskEither.right(value),
      ),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant