title: TaskCompletionSource for multiple objects awaiting same Task
body: <p>I am wondering if it would be appropriate to use the <code>TaskCompletionSource</code> class in the following example:</p>
  <p><strong>Original Code / Without TaskCompletionSource</strong></p>
  <pre><code>public async Task GetData() 
  {
      if (!IsLoaded) 
      {
          MyList = await Mediator.Send(new GetData());
          IsLoaded = true;
      }
  }
  </code></pre>
  <p>This function is available as part of a singleton scoped service within my .NET Core Web application.  It is possible for multiple objects to use the records in <code> MyList</code> and will await the <code>GetData()</code> function before using the list.</p>
  <p>You can see from the sample function, that it is possible for the method to be called multiple times while the first call is still awaiting the result from the Mediator request.  I want a solution to allow multiple objects to await the same Task. So is it appropriate to use a <code>TaskCompletionSource</code> object for this purpose?</p>
  <p><strong>Refactored Using TaskCompletionSource</strong></p>
  <pre><code>private TaskCompletionSource getDataTask;
  
  public async Task VerifyDataIsLoaded() 
  {
      if (getDataTask == null) 
      {
          getDataTask = new TaskCompletionSource();
          Task.Factory.StartNew(async () =&gt; 
          {
              await GetData();
              getDataTask.TrySetResult();
          }
      }
      return getDataTask.Task;
  }
  
  private async Task GetData() 
  {
      MyList = await Mediator.Send(new GetData());
  }
  </code></pre>
  <p>This should result in the first call to <code>VerifyDataIsLoaded()</code> instantiating an instance of the <code>TaskCompletionSource getDataTask</code> object, kicking off the <code>GetData()</code> function in a new threaded Task, and returning the <code>getDataTask.Task</code> property.</p>
  <p>Now if any other objects call into <code>VerifyDataIsLoaded()</code> while the <code>GetData()</code> function is awaiting the return from the <code>Mediator</code> object, they will all await the same Task, and prevent any redundant calls to <code>GetData</code>.</p>
  <p>Thoughts?? ... bad idea?? better way??</p>