

Our journey to F#: making async understand Tasks
source link: https://www.planetgeek.ch/2021/04/22/our-journey-to-f-making-async-understand-tasks/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Our journey to F#: making async understand Tasks

In our F# code, we have a lot of asynchronicity: query the DB, call external services, write messages to the service bus, etc. Inside of our happy F# bubble we use async workflows, either by using Async
or by using the computation expression async { ... }
. But we are surrounded by a world of Task
s. So we often need to call functions or methods that return a Task
. Of course, that is no big problem, we can just add |> Async.AwaitTask
, but that leads to a lot of visual clutter in our precious business logic code. And it’s very easy to extend the async { }
computation expression to do it for us.
Instead of writing
let foo () =
async {
let! bar = Bar.get () |> Async.AwaitTask
// do some stuff
return result;
}
we would like to write
let foo () =
async {
let! bar = Bar.get ()
// do some stuff
return result;
}
because the noise of switching from Task
to Async
disappears.
To achieve this, we added the following module to our code base:
[<AutoOpen>]
module Calitime.TimeRocket.AsyncCE
open System.Threading.Tasks
/// extends the async CE to allow awaiting tasks
type AsyncBuilder with
member x.Bind(task : Task<'a>, f : 'a -> Async<'b>) =
let task = task |> Async.AwaitTask
async.Bind(task, f)
The module above simply extends the AsyncBuilder
with an additional method that handles banging (!
) tasks.
Find all blog posts about our journey to F# here.
This blog post is made possible with the support of Time Rocket, the product this journey is all about. Take a look (German only).
Recommend
-
54
Flutter, a relatively new cross-platform framework backed by Google, has become very popular over the last 18 months. A Quick look at the steep rise of Stack Overflow questions demonstrates how quickly Flutter ha...
-
6
The Japanese Art of Making Boring Tasks BetterEven if your work feels mundane, there’s a way to find meaning in itIn Japan, phenomenal customer service is not limited to just high-end luxury shops and h...
-
6
Why trying to understand LLVM-IR So, a few days ago I saw some tweet saying about Reversing a Rust binary claiming it...
-
7
-
10
Opinions expressed in this article are those of the sponsor. 5 ways data can help you understand your buyer’s journey now...
-
9
InfluxData sponsored this post. Despite the term async and its association with asynchronous network I/O, this blog post argues that the
-
6
How to understand every employe...
-
8
HeySyncAsync tasks sync'd to your calendarFree OptionsYour sidekick to track & manage your async tasks. Simply share your HeySync link with your c...
-
6
Understand the Changes to the Customer Journey Complexity Score Previous year, we released a feature that allows customers to
-
6
Tasks & Phone Calls In A Real Time Marketing Journey 2023-06-01 Categories:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK