3 The async and await keywords

 

This chapter covers

  • Task and Task<T>
  • Using Task and Task<T> to check if an operation has completed
  • Using Task and Task<T> to notify your code when the operation has completed
  • Using Task and Task<T> in synchronous code
  • How async-await works

In the previous chapter we looked at how the compiler can transform our code in order to add language features. In this chapter we’ll see how it applies to async-await.

Async-await is a feature that let us write asynchronous code as if it was normal synchronous code. Why is this such a big deal? With asynchronous programming when we perform an operation that would normally make the CPU wait, usually for data to arrive from some device (for example, reading a file), instead of waiting we just do something else. Traditionally you need to divide each sequence of operations into little parts (breaking at each asynchronous operation) and call the right part at the part time, unsurprisingly this makes the code confusing to write.

To demonstrate this, I want to place the first and last diagrams from chapter 1 side by side:

Figure 3.1 Logical flow vs. running asynchronously

It’s easy to see that the left side that is describes the logical flow is simple, linear and easy to understand, while the right side that describes how the asynchronous version is running is none of those things (it’s also very difficult to debug).

3.1 Introducing Task and Task<T>

3.1.1 Are we there yet?

3.1.2 Wake me up when we get there

3.1.3 The synchronous option

3.1.4 After the task has completed

3.2 How does async-await work?

3.3 And what about multithreading?

3.4 Summary

sitemap