Skip to content

Convenience function to move AsyncResult from "railway world" back to "exception world" #341

@njlr

Description

@njlr

Is your feature request related to a problem? Please describe.

Exceptions are an important part of the F# ecosystem.

Sometimes error-handling is best done "railway style" and sometimes it's better to use exceptions. And sometimes we want different styles in different areas of a program.

We already have a way to go from "exception world" to "railway world", using AsyncResult.catch.

This feature request is to include a small helper function for going the other way: Async<Result<'ok, exn>> -> Async<'ok>.

Describe the solution you'd like

Add a helper function for this.

For example:

module AsyncResult =

  open System.Runtime.ExceptionServices

  let orReraise (workflow : Async<Result<'a, exn>>) : Async<'a> =
    async {
      match! workflow with
      | Ok a ->
        return a
      | Error exn ->
        ExceptionDispatchInfo.Capture(exn).Throw()
        return Unchecked.defaultof<_>
    }

Describe alternatives you've considered

Everyone writes their own!

Additional context

None

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