Introducing async-await
So far, we have discussed writing asynchronous code using tasks and how the TPL simplifies creating and managing tasks. However, tasks primarily rely on continuation, callbacks, or events to continue execution after the completion of a task. In enterprise applications, managing such code would be difficult; any runtime exceptions would be difficult to debug if too many tasks were chained. That's where C# comes in with async-await, a language feature introduced in C# 5.0 that simplifies the writing of asynchronous code, makes it more readable and maintainable, improves exception handling, and makes things easy to debug. So, let's dive into async-await.
async
is a keyword in C# that is used as a modifier and when prefixed to any method (or lambda) converts a method into a state machine, enabling the method to use the await
keyword in its body.
await
is a keyword in C# that is used as an operator and is followed by an expression that returns an...