question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using task instead of async, and offering query functions that fail hard instead of returning Result

See original GitHub issue

I am using the library for accessing data for a Giraffe web application, and it’s a pleasure to use so far! However, two things come to mind:

  1. As Giraffe switched to https://github.com/rspeele/TaskBuilder.fs some time ago, it would be nice to also get tasks out of it.
  2. In the context of a mobile app I would always prefer Result, however, for a web app I’d like to fail it hard and immediately -> throw exceptions.

So what I am doing right now is that:

module Sql =
    let throwOrReturn (result: Async<Result<'a, exn>>) =
        task {
            let! result = result |> Async.StartImmediateAsTask
            match result with
            | Ok result -> return result
            | Error exn -> return raise exn
        }

module FooRepository =
    let all =
        connect()
        |> Sql.query "..."
        ...
        |> Sql.throwOrReturn

I evaluate the result directly and either return the value or raise the exception. That’s not perfect for many reasons, but the only workaround that comes to mind right now.

I also tried to make the adjustments as suggested myself, but as some things are private I cannot extend the existing library as I wish I could:

image

I see that switching from async to task would be a massive breaking change. Maybe it would be possible to add it as as second option. As far as I can see, the parts within async { ... } would also work in task { ... }, so it wouldn’t even require code duplication. Update: Forget about this, that’s probably not going to work.

The same goes for functions that do not return Result. Those could be used inside of the existing ones and a try ... with block.

What do you think?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Zaid-Ajajcommented, Dec 5, 2020

It might be worth it for the library to consider using ExceptionDispatchInfo.Capture as a way to handle exceptions so capturing the stacktrace remains accurate when a consumer throws it.

@TheAngryByrd Smells like a PR, doesn’t it? 😄 Would really appreciate it if you send one since we are still using Result in the Npgsql.FSharp namespace

0reactions
Zaid-Ajajcommented, Dec 12, 2020

Resolved as of v3.12.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

When is the best place to use Task.Result instead of ...
Use an asynchronous wait to improve efficiency. Remember, await simply means "this workflow cannot progress further until this task is completed ...
Read more >
Task asynchronous programming model
Learn when and how to use Task-based async programming, a simplified approach to asynchronous programming in C#.
Read more >
Common async / Task mistakes, and how to avoid them
Now, look at this code instead async Task Main() { DoStuff(); }async Task DoStuff() { await Task.Delay(100); throw new Exception(); }.
Read more >
Using promises - JavaScript - MDN Web Docs
A Promise is an object representing the eventual completion or failure of an asynchronous operation. Since most people are consumers of ...
Read more >
Async IO in Python: A Complete Walkthrough
This tutorial will give you a firm grasp of Python's approach to async IO, which is a concurrent ... Using await and/or return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found