Giter Site home page Giter Site logo

Comments (7)

JordanMarr avatar JordanMarr commented on July 21, 2024 2

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
    

from sqlhydra.

stoft avatar stoft commented on July 21, 2024 2

Thank you for the insights, very helpful for my understanding. πŸ™‚

from sqlhydra.

JordanMarr avatar JordanMarr commented on July 21, 2024 1

I almost always add my own Task.fs with Task.map to my data projects since it’s only a few lines.

Alternatively, there is also the non async overload of Insert if you don’t care about async which blocks for you and automatically unwraps the task.

Also, you are still free to use TaskBuilder in your project -- I just removed the dependency so that you are not required to use it (since some people are already using F# 6.0, which would make TaskBuilder redundant for them).

from sqlhydra.

JordanMarr avatar JordanMarr commented on July 21, 2024

Hi!

That is a great question. I chose to work with tasks because

  • The ADO.NET libraries use tasks
  • SqlKata also uses tasks
  • F# 6 comes with a brand new TaskBuilder out-of-the-box which will make working with tasks even better

All of other the QueryContext data methods use tasks. The method you referenced happen to be the few places where I actually needed to await a result, therefore it was necessary to call Async.AwaitTask (see the F# Async Programming docs).
For those cases, I also had to call StartImmediateAsTask; otherwise, they would be the only methods that return async instead of task.

One other point of interest is that I initially used a third party TaskBuilder library, but then decided that there weren't enough calls to justify forcing that dependency.

from sqlhydra.

stoft avatar stoft commented on July 21, 2024

Is this different from accessing Task.Result? "If the Result property is accessed before the computation finishes, the property blocks the calling thread until the value is available."

from sqlhydra.

JordanMarr avatar JordanMarr commented on July 21, 2024

Yes, this approach is actually asynchronous, whereas Task.Result would not be because it would block (wait) for the database response before returning the result.

from sqlhydra.

stoft avatar stoft commented on July 21, 2024

And thus it's up to the caller of InsertAsync to block or not? For lack of a Task.map or similar (which a task builder lib or v6 would provide). Makes sense. πŸ˜„

from sqlhydra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.