# How to use task builder right

**URL:** <https://forums.fsharp.org/t/how-to-use-task-builder-right/3394>\
**Category:** General\
**Tags:** beginners\
**Created:** [October 30, 2023, 7:32am UTC](https://forums.fsharp.org/t/how-to-use-task-builder-right/3394 "2023-10-30T07:32:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![altbodhi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/altbodhi/32/1253_2.png) [@altbodhi](https://forums.fsharp.org/u/altbodhi)\
**Post date:** [October 30, 2023, 7:32am UTC](https://forums.fsharp.org/t/how-to-use-task-builder-right/3394/1 "2023-10-30T07:32:47Z")

</div>

I do not understand how to use task builder right. How to get result correctly and elegant?

```auto
let t = task { return! someTaks() } 
let r = t.Result // or
t.Wait()
let r = t.Result // or
let r = t |> Async.AwaiTask |> Async.RunSynchronously
// or any?

```

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [November 1, 2023, 6:15pm UTC](https://forums.fsharp.org/t/how-to-use-task-builder-right/3394/2 "2023-11-01T18:15:51Z")

</div>

Whew that’s kind of a big topic!  
For starters, there’s two different types for doing mostly the same thing: `Async` and `Task`, and some of the confusion I think stems from why both exist. In short, F# made `Async` first, and then C# made `Task` later and F# had to support `Task` without getting rid of `Async`. Typically F#-only solutions tend to use `Async`, while solutions with both C# and F# mixed tend to use `Task` instead.

Secondly, one should have some care with `Async.RunSynchronously`, or any of the equivalents you’ve pointed out, since it effectively undoes all of the benefits of asynchronous programming. The official guidance is

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fsharp/original/2X/8/8164248992df8fc03cd0d820f08aa9f542eb8cc6.png)

- [Async and Task Programming - F# | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/fsharp/tutorials/async#asyncrunsynchronously)

If you’re not at the entry point for the executable, and you do care about performance, then there really isn’t a way to escape `Task`/`Async`. If you have some value `t : Task<int>` and you have a function `fn` that wants to use the `int` value in `t`,

```auto
let fn () = 
  t.Result + 5

```

then really you need to change `fn` to make it asyncronous as well

```auto
let fn () = task {
  let! value = t
  return value + 5
}

```

This should be the default approach in your code to make sure you’re getting the benefits of asynchronous programming.

So with all that out of the way, if you’re talking about the entry point of the application or you don’t care about performance, then you should use `_.Result` for a `Task<_>`, and you should use `_.Wait()` for a `Task` (or just in general don’t care about any value that is returned). If you’re working with `Async` instead of `Task`, then use `Async.RunSynchronously`. If your entire codebase is F#, then it’s probably more common to turn any `Task` you run into immediately into an `Async` via `Async.AwaitTask`.

---

<div class="post-metadata">

**Author:** ![realparadyne](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/realparadyne/32/169_2.png) [@realparadyne](https://forums.fsharp.org/u/realparadyne)\
**Post date:** [November 3, 2023, 6:18pm UTC](https://forums.fsharp.org/t/how-to-use-task-builder-right/3394/3 "2023-11-03T18:18:37Z")

</div>

> [@altbodhi](#):
>
> `let r = t.Result`

Assuming you really do want to block the thread and wait synchronously for the task to finish and get the result, then the first option you gave is the right one to use. It will internally do the t.Wait() if the Task is still running. If the Task completes with an exception, it will be thrown at the point of the t.Result property access.
