title: How to implement async/await syntax properly using Boost.Asio
body: <p>I'm trying to implement some network application using Boost.Asio. I have a problem with multiple layers of callbacks. In other languages that natively support <code>async/await</code> syntax, I can write my logic like this</p>
  
  <pre><code>void do_send(args...) {
    if (!endpoint_resolved) {
      await resolve_async(...);  // results are stored in member variables
    }
    if (!connected) {
      await connect_async(...);
    }
    await send_async(...);
    await receive_async(...);
  }
  </code></pre>
  
  <p>Right now I have to write it using multiple layers of callbacks</p>
  
  <pre><code>void do_send(args...) {
    if (!endpoint_resolved) {
      resolve_async(..., [captures...](args...) {
        if (!connected) {
          connect_async(..., [captures...](args...) {
            send_async(..., [captures...](args...) {
              receive_async(..., [captures...](args...) {
                // do something
              });  // receive_async
            });  // send_async
          });  // connect_async
        }
      });
    }
  }
  </code></pre>
  
  <p>This is cumbersome and error-prone. An alternative is to use <code>std::bind</code> to bind member functions as callbacks, but this does not solve the problem because either way I have to write complicated logic in the callbacks to determine what to do next.</p>
  
  <p>I'm wondering if there are better solutions. <strong>Ideally I would like to write code in a synchronous way while I can <code>await</code> asynchronously on any I/O operations.</strong></p>
  
  <p>I've also checked <code>std::async</code>, <code>std::future</code>, etc. But they don't seem to fit into my situation.</p>