demystifyfp/FsToolkit.ErrorHandling

TaskResult.ofTask?

njlr opened this issue · 2 comments

njlr commented

Is there a function to catch a Task and turn it into a TaskResult?

Something like this:

module TaskResult = 

  let ofTask (t : Task<'a>) = 
    task {
      try
        let! x = t

        return Ok t
      with exn ->
        return Error exn
    }

I'm not sure how well this would work given that tasks are "hot".

Not a direct function but you could use

myTask
|> Task.catch
|> Task.map Result.ofChoice

That being said, I'd recommend using a taskResult CE with a try/with as I don't want to implement every possible little conversion in function form.

  1. Naming is hard and setting expectations of what a function does is even harder. There's already and TaskResult.ofTask but it doesn't do a catch. Should it? It's hard to know what every expects each function to do.
  2. This README shows the ever growing giant list of functions that are across the different libraries already.
  3. Computation expressions have a Source overload which cuts down on maintaining every little tiny conversion.