12 Asynchronous programming

 

This chapter covers

  • Understanding what asynchronous programming is
  • Asynchronous programming in F# and .NET
  • Differences between the Async and Task abstractions
  • Immutable data

Asynchronous (async) programming has become commonplace in the world of software development in the last few years. If you want to do any form of web development or distributed programming, you need to understand what it is and how to take advantage of it (luckily, F# has you covered here and has excellent support).

Bear in mind, though: asynchronous programming can be a very complicated subject. It’s not just the programming model but the idea of reasoning about multiple things working in parallel, or the idea of long-running background processes, or the fact that the topic of distributed programming often gets interwoven with background async workloads. It can be really difficult. I won’t try to unravel all its mysteries in this chapter; entire books are dedicated to it. Instead, I want to focus on practical usability in F# and some patterns to get you started.

12.1 What is asynchronous programming?

Despite what I’ve just said, this section will give you a basic overview of what asynchronous means and help you identify when it is useful (or necessary!). Feel free to skip this next section if you’re already confident with the idea of asynchronous programming, such as working with async/await or promises.

12.1.1 Async and sync programming

12.1.2 A brief history of async on .NET

12.2 Asynchronous support in F#

12.2.1 Tasks in .NET

12.2.2 Consuming Tasks

12.2.3 The task { } block: F#’s async/await

12.2.4 Executing multiple Tasks in parallel

12.2.5 Benefits of Tasks’ computation expressions

12.2.6 The async block

12.3 Final thoughts on asynchronous workloads

12.3.1 The importance of immutable data

12.3.2 The viral nature of asynchronous workloads

Summary