Still learning about async-await. I bumped into examples similar to following:
public async Task MethodAsync()
{
await Method01Async();
await Method02Async();
}
What is the purpose of the last await? Method02Async is the last line of MethodAsync method. So there is no any method remainder – no any lines below – no anything to be called in the callback generated by the compiler… Am I missing anything?
There actually is a “method remainder” – it completes the
Taskreturned byMethodAsync.(The return value of)
Method02Asyncis awaited so thatMethodAsyncis not completed untilMethod02Asynccompletes.If you had:
Then the
MethodAsyncwill (asynchronously) wait forMethod01Asyncto complete and then startMethod02Async.MethodAsyncwill then complete whileMethod02Asyncmay still be in progress.The way you have it:
Means that
MethodAsyncwill (asynchronously) wait forMethod01Asyncto complete and then (asynchronously) wait forMethod02Asyncto complete, and only then willMethodAsynccomplete.