title: Combining functions that return futures
body: <p>Suppose I have two functions that return futures:</p>
  
  <pre><code>std::future&lt;T&gt; foo(int);
  
  std::future&lt;U&gt; bar(T const &amp;);
  </code></pre>
  
  <p>I want to combine the two functions to a function that takes an <code>int</code> as parameter and returns a <code>std::future&lt;U&gt;</code>. How should I write this function? Is it possible to generalize function composition for functions that return futures? </p>
  
  <pre><code>std::future&lt;U&gt; foobar1(int x)
  {
    auto foo_x = foo(x);
    return bar(foo_x.get());
  }
  </code></pre>
  
  <p>This function will block until the future returned by <code>foo</code> is finished, right? This is clearly not what I want.</p>
  
  <pre><code>std::future&lt;U&gt; foobar2(int x)
  {
    return std::async([=]()
    {
      auto foo_x = foo(x);
      return bar(foo_x.get()).get();
    });
  }
  </code></pre>
  
  <p>It feels silly to call <code>get()</code> on the future returned by <code>bar</code> just to have it turned into a new future by <code>std::async</code></p>
  
  <pre><code>std::future&lt;U&gt; foobar3(int x)
  {
    return foo(x).then([](std::future&lt;T&gt; f)
    {
      return bar(f.get()).get();
    };
  }
  </code></pre>
  
  <p>Here, again I have to call <code>get()</code> on the future returned by <code>bar</code> otherwise I'd have a <code>future&lt;future&lt;U&gt;&gt;</code>. Is this the correct approach?</p>