title: Why does this async/await code NOT cause a deadlock?
body: <p>I have found the following example in Jon Skeet's "C# in depth. 3rd edition":</p>
  
  <pre><code>static async Task&lt;int&gt; GetPageLengthAsync(string url)
  {
      using (HttpClient client = new HttpClient())
      {
          Task&lt;string&gt; fetchTextTask = client.GetStringAsync(url);
          int length = (await fetchTextTask).Length;
          return length;
      }
  }
  
  public static void Main()
  {
      Task&lt;int&gt; lengthTask = GetPageLengthAsync("http://csharpindepth.com");
      Console.WriteLine(lengthTask.Result);
  }
  </code></pre>
  
  <p>I expected that this code would deadlock, but it does not.  </p>
  
  <p>As I see it, it works this way:</p>
  
  <ol>
  <li><code>Main</code> method calls <code>GetPageLengthAsync</code> synchronously within the main thread.  </li>
  <li><code>GetPageLengthAsync</code> makes an asynchronous request and immediately returns <code>Task&lt;int&gt;</code> to <code>Main</code> saying "wait for a while, I will return you an int in a second". </li>
  <li><code>Main</code> continues execution and stumbles upon <code>lengthTask.Result</code> which causes the main thread to block and wait for <code>lengthTask</code> to finish its work.</li>
  <li><code>GetStringAsync</code> completes and waits for main thread to become available to execute <code>Length</code> and start continuation.</li>
  </ol>
  
  <p>But it seems like I misunderstand something. Why doesn't this code deadlock?</p>
  
  <p>The code in <a href="https://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock">this StackOverflow question about await/async deadlock</a> seems to do the same, but deadlocks.  </p>