From 931a288b0e12fa547d0d8ccc4bad6b3a01704a27 Mon Sep 17 00:00:00 2001 From: otto-dev Date: Sat, 16 Apr 2022 08:07:03 +1000 Subject: [PATCH 1/3] Add catchAsync + tests --- lib/src/either.dart | 8 ++++++++ test/either_test.dart | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/src/either.dart b/lib/src/either.dart index 2ee5b58..ee8dbd2 100644 --- a/lib/src/either.dart +++ b/lib/src/either.dart @@ -213,6 +213,14 @@ Either catching(Function0 thunk) { } } +Future> catchAsync(Function0> f) async { + try { + return Right(await f()); + } catch (e) { + return Left(e); + } +} + class EitherMonad extends MonadOpsMonad> { EitherMonad(): super(right); } diff --git a/test/either_test.dart b/test/either_test.dart index 6461710..e2c66c5 100644 --- a/test/either_test.dart +++ b/test/either_test.dart @@ -103,4 +103,20 @@ void main() { expect(right.value, 2); }); }); + + group("catchAsync", () { + test("successful future", () async { + expect(await catchAsync(() => Future.value(2)), right(2)); + }); + + test("failed future", () async { + final error = Exception("foobar"); + expect(await catchAsync(() => Future.error(error)), left(error)); + }); + + test("throwing synchronous function body", () async { + final error = Exception("foobar"); + expect(await catchAsync(() => throw error), left(error)); + }); + }); } From bf4fb166b9783ad72cb35cf25002eafbc4b32631 Mon Sep 17 00:00:00 2001 From: otto-dev Date: Sat, 16 Apr 2022 08:12:42 +1000 Subject: [PATCH 2/3] use lowercase either constructors --- lib/src/either.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/either.dart b/lib/src/either.dart index ee8dbd2..eca9353 100644 --- a/lib/src/either.dart +++ b/lib/src/either.dart @@ -215,9 +215,9 @@ Either catching(Function0 thunk) { Future> catchAsync(Function0> f) async { try { - return Right(await f()); + return right(await f()); } catch (e) { - return Left(e); + return left(e); } } From fcb65dd1426bf33c064a84419bf39a70632fccc5 Mon Sep 17 00:00:00 2001 From: otto-dev Date: Sat, 16 Apr 2022 08:17:07 +1000 Subject: [PATCH 3/3] Allow method bodies that return FutureOr --- lib/src/either.dart | 2 +- test/either_test.dart | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/src/either.dart b/lib/src/either.dart index eca9353..774f814 100644 --- a/lib/src/either.dart +++ b/lib/src/either.dart @@ -213,7 +213,7 @@ Either catching(Function0 thunk) { } } -Future> catchAsync(Function0> f) async { +Future> catchAsync(Function0> f) async { try { return right(await f()); } catch (e) { diff --git a/test/either_test.dart b/test/either_test.dart index e2c66c5..e1be8da 100644 --- a/test/either_test.dart +++ b/test/either_test.dart @@ -118,5 +118,13 @@ void main() { final error = Exception("foobar"); expect(await catchAsync(() => throw error), left(error)); }); + + test("value from async function", () async { + expect(await catchAsync(() async => 2), right(2)); + }); + + test("value from sync function", () async { + expect(await catchAsync(() => 2), right(2)); + }); }); }