Async, await, and Task in C#

async await

Asynchronous programming is one of the powerful features in C#. It allows parallelism in programming which results in faster execution of a program. There are many languages that support asynchronous programming. Examples can be thread class in java, promises in Javascript, and GoRoutines in Go language.
In C# to achieve this, we use 3 keywords async, await, and Task.

async: This keyword allows us to make the method asynchronous and enable us to use await inside the program. Methods declared as async can be executed asynchronously.

Task: A task is a wrapper over a piece of async code or function, methods declared as async returns Task or Task<T> where T is the desired output. It’s like the promise of javascript a placeholder for the execution of the code, which doesn’t have the result yet.

await: Await allows us to wait for the task to complete along with that await allows us to convert Task<T> to T. This is like .then() of javascript which we use on promises Promise.then(). This ensures that the task is completed and you can proceed with the program.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *