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.

Conversion from Task to Async to Task

See original GitHub issue

Hello! I noticed that there is a conversion from Task to Async to Task here: https://github.com/JordanMarr/SqlHydra/blob/7d24b1d1b8266786bb3e03a17f3e6b214e128a59/src/SqlHydra.Query/QueryContext.fs#L127

and here: https://github.com/JordanMarr/SqlHydra/blob/7d24b1d1b8266786bb3e03a17f3e6b214e128a59/src/SqlHydra.Query/QueryContext.fs#L136

Just for my understanding, is this double conversion really necessary? And possibly correlated, why choose to return a Task and not an Async?

Cheers!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
stoftcommented, Oct 21, 2021

Thank you for the insights, very helpful for my understanding. 🙂

2reactions
JordanMarrcommented, Oct 20, 2021

For your convenience, this is the one I created for my current project. I considered adding some of this stuff to the library, but wanted to wait and see what is included in F# 6.0 first.

/// Utility functions for working with tasks.
module Task

open FSharp.Control.Tasks.V2
open System.Threading.Tasks

let bind (f : 'a -> Task<'b>) (x : Task<'a>) = 
    task {
        let! x = x
        return! f x
    }

/// Maps the result of Task<'a> to Task<'b>
let map<'a, 'b> (f : 'a -> 'b) (tsk: Task<'a>) =
    task {
        let! taskValue = tsk
        return f taskValue
    }

let mapSeq<'a, 'b> (f : 'a -> 'b) (itemsTask: Task<'a list>) =
    task {
        let! items = itemsTask
        return items |> Seq.map f
    }

let mapArray<'a, 'b> (f : 'a -> 'b) (itemsTask: Task<'a seq>) =
    task {
        let! items = itemsTask
        return items |> Seq.map f |> Seq.toArray
    }

let awaitIgnore (tsk: Task<'T>) =
    task {
        let! result = tsk
        result |> ignore
    }
    :> Task

let fromResult value = 
    System.Threading.Tasks.Task.FromResult value
    
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Convert any given function into an awaitable task
The goal of the following code is to cast any given function into an awaitable function. The idea is to use it when...
Read more >
7 tips for converting C# code to async/await
Every async method returns a Task . Use Task when there is no specific result for the method, which is synonymous with void...
Read more >
7 tips for converting C# code to async/await - Jamie Magee
Every async method returns a Task . Use Task when there is no specific result for the method, which is synonymous with void...
Read more >
Async-Sync conversion. : r/csharp
Async -Sync conversion. Say that we have two methods: public int DoSomething() { //implemented method } public async Task<int> ...
Read more >
Implicit conversion from T to Task<T> · Issue #15892
I would step back and look at it this way: If the method isn't async and isn't waiting on something else, why is...
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