Skip to content

Question: get the first success from Async<Result<'ok, 'error>> list? #340

@njlr

Description

@njlr

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions