title: C++ Asynchronous creation of objects
body: <p>I currently have an asynchronous API where a Manager class is responsible of creating other objects of class <code>A</code>, <code>B</code>, <code>C</code>, etc. The creation is asynchronous, and every call to a function <code>manager.createXXX()</code> will be accompanied by its callback <code>on_createXXX_done(A&amp; a)</code>.</p>
  
  <p>The creation of the <code>A</code>, <code>B</code>, <code>C</code> objects need to be sequential. And a typical use of the API will involve calling the creation of <code>A</code>, and in its callback, triggering the creation of <code>B</code>, and so on.</p>
  
  <p>This involves having the creation logic split into many functions, which I find ugly. Furthermore, operations that need to be performed once all the objects are created will need to be coded on the callback of the last created object (<code>C</code>), even if there is no direct relation between that object and the operation to perform.</p>
  
  <p>Builder design pattern does not actually fit since manager does not own the objects <code>A</code>, <code>B</code>, <code>C</code>, it just helps on the creation and return them.</p>
  
  <pre><code>void on_creationA_done(A&amp; a)
  {
      m_a = a;
      //Trigger next creation
      m_manager.createB()
  }
  void on_creationB_done(B&amp; b)
  {
      m_b = b;
      //Trigger next creation
      m_manager.createC()
  }
  
  void on_creationC_done(C&amp; c)
  {
      m_c = c;
      //No more creations are needed. Just perform some post-creation actions  
      do_something() // This needs to be called once all A, B, C are created.
  }
  </code></pre>
  
  <p>I have also checked folly/futures to chain asynchronous operations but that would involved exposing folly library through our public API, right?</p>
  
  <p>What would be a good pattern to use in this case?</p>