FsToolkit.ErrorHandling
FsToolkit.ErrorHandling copied to clipboard
Question: get the first success from `Async<Result<'ok, 'error>> list`?
Like List.sequenceAsyncResultM but would stop at the first success.
Is there something like this already provided?
let firstSuccess (tasks : Async<Result<'ok, 'error>> list) : Async<Result<'ok, 'error list>> =
let rec loop (errors : 'e list) tasks =
async {
match tasks with
| [] ->
return Error (List.rev errors)
| x :: xs ->
match! x with
| Ok t ->
return Ok t
| Error error ->
return! loop (error :: errors) xs
}
loop [] tasks
If not, would a PR be accepted?
Thanks!
I don't think anything like this is available currently. It feels like a derivative of List.tryPick but accumulates errors. Maybe we should make that available with an additional helper to yield if the list is already a group
module List =
module AsyncResult =
let tryPick f tasks =
async {
let mutable errors = ListCollector()
let mutable result = ValueNone
for task in tasks |> List.takeWhile (fun _ -> result.IsNone) do
match! f task with
| Ok t -> result <- ValueSome(Ok t)
| Error error -> errors.Add error
match result with
| ValueSome okResult -> return okResult
| ValueNone -> return Error(errors.Close())
}
let tryYield tasks = tryPick id tasks
It feels like a derivative of List.tryPick but accumulates errors.
Hmmm does this make it part of AsyncValidation then?
Hmmm does this make it part of AsyncValidation then?
Yeah probably.