Await on the last method line

后端 未结 1 1529
旧巷少年郎
旧巷少年郎 2021-02-05 09:23

Still learning about async-await. I bumped into examples similar to following:

public async Task MethodAsync()
{
  await Method01Async();
  await Method02Async()         


        
1条回答
  •  梦谈多话
    2021-02-05 09:57

    There actually is a "method remainder" - it completes the Task returned by MethodAsync.

    (The return value of) Method02Async is awaited so that MethodAsync is not completed until Method02Async completes.

    If you had:

    public async Task MethodAsync()
    {
      await Method01Async();
      Method02Async();
    }
    

    Then the MethodAsync will (asynchronously) wait for Method01Async to complete and then start Method02Async. MethodAsync will then complete while Method02Async may still be in progress.

    The way you have it:

    public async Task MethodAsync()
    {
      await Method01Async();
      await Method02Async();
    }
    

    Means that MethodAsync will (asynchronously) wait for Method01Async to complete and then (asynchronously) wait for Method02Async to complete, and only then will MethodAsync complete.

    0 讨论(0)
提交回复
热议问题