Skip to content
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

Add Try to Do notation #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/fpdart/lib/src/task_either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ final class TaskEither<L, R> extends HKT2<_TaskEitherHKT, L, R>
}
});

/// Initialize a **Try to Do Notation** chain.
// ignore: non_constant_identifier_names
factory TaskEither.TryToDo(
DoFunctionTaskEither<L, R> f,
L Function(Object error, StackTrace stackTrace) onError,
) =>
TaskEither(() async {
try {
return Either.of(await f(_doAdapter<L>()));
} on _TaskEitherThrow<L> catch (e) {
return Either.left(e.value);
} catch (error, stack) {
return Left<L, R>(onError(error, stack));
}
});

/// Used to chain multiple functions that return a [TaskEither].
///
/// You can extract the value of every [Right] in the chain without
Expand Down