title: Chaining two functions () -> Task<A> and A->Task<B>
body: <p>I don't know if I am thinking in the wrong way about TPL, but I have difficulty understanding how to obtain the following:</p>
  
  <p>I have two functions</p>
  
  <pre><code>Task&lt;A&gt; getA() { ... }
  Task&lt;B&gt; getB(A a) { ... }
  </code></pre>
  
  <p>This seems to occur often: I can asyncronously get an A. And given an A, I can asynchronously get a B.</p>
  
  <p>I can't figure out the correct way to chain these functions together in TPL.</p>
  
  <p>Here is one attempt:</p>
  
  <pre><code>Task&lt;B&gt; Combined()
  {
      Task&lt;A&gt; ta = getA();
      Task&lt;Task&lt;B&gt;&gt; ttb = ta.ContinueWith(a =&gt; getB(a.Result));
      return ttb.ContinueWith(x =&gt; x.Result.Result);
  }
  </code></pre>
  
  <p>The <code>ContinueWith</code> is where I get confused. The returned type is a "double-Task", <code>Task&lt;Task&lt;B&gt;&gt;</code>. This somehow just seems wrong to me.</p>
  
  <p><strong>UPDATE 2011-09-30:</strong></p>
  
  <p>By coincidence I found the extension method <code>TaskExtensions.Unwrap</code> that operates on a <code>Task&lt;Task&lt;T&gt;&gt;</code> to give a <code>Task&lt;T&gt;</code>.
  So until we get C# 5.0, I can do ta.ContinueWith(a=>...).UnWrap() in situations like this where the continuation itself returns a task.</p>