title: Want to understand async
body: <p>I've used async coding a little bit but I don't really fully understand how to use it -- though I understand the concept and why I need it.</p>
  
  <p>Here's my set up:</p>
  
  <p>I have a Web API that I will call from my ASP.NET MVC app and my Web API will call DocumentDB. In code samples, I see a lot of await keywords while sending queries to DocumentDB.</p>
  
  <p>I'm confused if I need to make my Index action method in my MVC app async?
  I'm also confused if my CreateEmployee() method in my Web API should be async?</p>
  
  <p>What is the right way to use async in this scenario?</p>
  
  <p>Here's my code (This code is currently giving me errors because my MVC action method is not async)
  ---- ASP.NET MVC App Code ----</p>
  
  <pre><code>public ActionResult Index()
  {
  
     Employee emp = new Employee();
     emp.FirstName = "John";
     emp.LastName = "Doe";
     emp.Gender = "M";
     emp.Ssn = "123-45-6789";
  
     using (var client = new HttpClient())
     {
        client.BaseAddress = new Uri("http://myWebApi.com");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
        HttpResponseMessage response = await client.PostAsJsonAsync("hr/create/newemployee", emp);
        if (response.IsSuccessStatusCode)
        {
           emp = await response.Content.ReadAsAsync&lt;Employee&gt;();
        }
     }
  
     // Display employee info
     return View(emp);
  }
  </code></pre>
  
  <p>---- Web API Code ----</p>
  
  <pre><code>private static readonly string endPointUrl = ConfigurationManager.AppSettings["EndPointUrl"];
  private static readonly string authorizationKey = ConfigurationManager.AppSettings["AuthorizationKey"];
  private static readonly string databaseId = ConfigurationManager.AppSettings["DatabaseId"];
  private static DocumentClient client;
  
  public static async Task&lt;Employee&gt; CreateEmployee(Employee emp)
  {
     try
     {
        //Create a Document client
        using (client = new DocumentClient(new Uri(endPointUrl), authorizationKey))
        {
           //Get the database
           var database = await GetDatabaseAsync();
  
           //Get the Document Collection
           var collection = await GetCollectionAsync(database.SelfLink, "Employees");
  
           await client.CreateDocumentAsync(collection.SelfLink, emp);
  
           // Further process employee
         }
      }
      catch
      {
         // Handle error
      }
  
      return employee;
  }
  
  private static async Task&lt;DocumentCollection&gt; GetCollectionAsync(string dbLink, string id)
  {
     DocumentCollection collection = client.CreateDocumentCollectionQuery(dbLink).Where(c =&gt; c.Id == id).ToArray().FirstOrDefault();
  
     return collection;
  }
  
  private static async Task&lt;Database&gt; GetDatabaseAsync()
  {
     Database database = client.CreateDatabaseQuery().Where(db =&gt; db.Id == databaseId).ToArray().FirstOrDefault();
  
     return database;
  }
  </code></pre>