Skip to content

Commit a4379fe

Browse files
authored
Close issue #145 - add Result.traverseAsync (#153)
* feature: add traverseAsync function * feature: add traverseAsync tests
1 parent f893b51 commit a4379fe

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/FsToolkit.ErrorHandling/Result.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ module Result =
214214
| Error err -> return Error err
215215
}
216216

217+
///
218+
let traverseAsync (f: 't -> Async<'u>) (res: Result<'t, 'e>): Async<Result<'u, 'e>> =
219+
((Result.map f) >> sequenceAsync) res
220+
217221
/// Returns the Ok value or runs the specified function over the error value.
218222
let valueOr f res =
219223
match res with

tests/FsToolkit.ErrorHandling.Tests/Result.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,23 @@ let sequenceAsyncTests =
682682
Expect.equal value (Error "foo") ""
683683
} ]
684684

685+
let traverseAsyncTests =
686+
testList "traverseAsync Tests" [
687+
testCaseAsync "traverseAsync returns the async value if Ok" <| async {
688+
let resAsnc = async { return "foo" } |> Ok
689+
let resFunc = id
690+
let! value = (resFunc, resAsnc) ||> Result.traverseAsync
691+
Expect.equal value (Ok "foo") ""
692+
}
693+
694+
testCaseAsync "traverseAsync returns the error value if Error" <| async {
695+
let resAsnc = Error "foo"
696+
let resFunc = id
697+
let! value = (resFunc, resAsnc) ||> Result.traverseAsync
698+
Expect.equal value (Error "foo") ""
699+
}
700+
]
701+
685702
let valueOrTests =
686703
testList
687704
"valueOrTests Tests"
@@ -781,6 +798,7 @@ let allTests =
781798
teeErrorTests
782799
teeErrorIfTests
783800
sequenceAsyncTests
801+
traverseAsyncTests
784802
valueOrTests
785803
zipTests
786804
zipErrorTests ]

0 commit comments

Comments
 (0)