Snippets created by Aaron Eshbach

  • Await Computation Expression (Task + Async Interoperability)

    When writing asynchronous code in F#, one often needs to call methods in the .NET BCL that return Task or Task rather than the F# native Async<'a> type. While the Async.AwaitTask function can convert a Task into an Async<'a>, there is some cognitive overhead in having to keep track of which functions return Task and which functions are Async. To simplify this interoperability, this code provides new `await` computation expression that supports using both Async<'a> and Task or Task inside it. This works by unifying the Task and Async types in the Awaitable DU, and overloading the Bind, ReturnFrom, Combine, and Using methods on the AwaitableBuilder Computation Builder to handle Awaitable<'a>, Async<'a>, Task, and Task.

    3 people like this

    Posted: 21 days ago by Aaron Eshbach