Skip to content

Commit 54a73d7

Browse files
sheridanchrisTheAngryByrd
authored andcommitted
Add AsyncResult bindRequire functions with tests
1 parent 427d35e commit 54a73d7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/FsToolkit.ErrorHandling/AsyncResult.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,19 @@ module AsyncResult =
333333
let inline ofResult (x: Result<'ok, 'error>) : Async<Result<'ok, 'error>> =
334334
x
335335
|> Async.singleton
336+
337+
/// Bind the AsyncResult and requireSome on the inner option value.
338+
let inline bindRequireSome error x =
339+
x
340+
|> bind (
341+
Result.requireSome error
342+
>> Async.singleton
343+
)
344+
345+
/// Bind the AsyncResult and requireNone on the inner option value.
346+
let inline bindRequireNone error x =
347+
x
348+
|> bind (
349+
Result.requireNone error
350+
>> Async.singleton
351+
)

tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,27 @@ let zipErrorTests =
795795
]
796796

797797

798+
let asyncResultBindRequireTests =
799+
testList "AsyncResult Bind + Require Tests" [
800+
testCaseAsync "bindRequireNone"
801+
<| async {
802+
do!
803+
Some "john_doe"
804+
|> AsyncResult.ok
805+
|> AsyncResult.bindRequireNone "User exists"
806+
|> Expect.hasAsyncErrorValue "User exists"
807+
}
808+
809+
testCaseAsync "bindRequireSome"
810+
<| async {
811+
do!
812+
Some "john_doe"
813+
|> AsyncResult.ok
814+
|> AsyncResult.bindRequireSome "User doesn't exist"
815+
|> Expect.hasAsyncOkValue "john_doe"
816+
}
817+
]
818+
798819
let allTests =
799820
testList "Async Result tests" [
800821
mapTests
@@ -828,4 +849,5 @@ let allTests =
828849
asyncResultOperatorTests
829850
zipTests
830851
zipErrorTests
852+
asyncResultBindRequireTests
831853
]

0 commit comments

Comments
 (0)