Awaitable Types Revisited

Learn about using C++ coroutines for asynchronous programming and awaitable types, focusing on Boost.Asio and rudimentary task types.

Introduction

We already talked a bit about awaitable types in the previous chapter. But now we need to get a little bit more specific about what co_await does and what an awaitable type is. The keyword co_await is a unary operator, meaning that it takes a single argument. The argument we pass to co_await needs to fulfill some requirements that we will explore in this section.

When we say co_await in our code, we express that we are waiting for something that may or may not be ready. If it’s not ready, co_await suspends the currently executing coroutine and returns control back to its caller. When the asynchronous task has been completed, it should transfer the control back to the coroutine that is originally waiting for the task to finish. From here on, we will typically refer to the awaiting function as the continuation.

Now consider the following expression:

Get hands-on with 1400+ tech skills courses.