Await
This topic took me a long time to comprehend. Before reading this part, please ensure, you have read: Link to asynchronous explanation
What is async / await keyword ?
After reading the Explanation to asynchronous, you should now know, that they are processes, which do not depent on eachother and it does not matter which one is executed first.
The await operator ensures, that exactly this is done in dotnet. They are used in methods.
Look at this C# code:
public async Task RunningMethod(this List<int> integers)
{
int result = await integers.GetFirstNum();
Console.WriteLine(result);
}
public async Task<int> GetFirstNum(this List<int> integers)
{
await Task.Delay(1000); // Delays the task for 1 second
return integers.First();
}
As you see in the example we got the keywords: async
, await
and Task
.
Please remind those, they are really common. Here an explanation to each one of them:
async
: This is always used in combination withawait
. It indicates, that the Method has a Task which usesawait
await
: This indicates, that the following code in the same line, will take a bit longer and C# should already continue with the other methods. This is usually always seen withasync
Task
: This is a class provided by Dotnet. As the name says, it is a Task, which can be awaited, as you see in the code.