0Pricing
C++ Academy · Lesson

Async Tasks and Awaiter Types

Compose async tasks with custom awaiter and promise types.

Async Tasks and Awaiter Types is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Async Coroutines

An async task suspends on I/O and resumes when the operation completes. Combine co_await with asynchronous APIs for callback-free async code.

The Task Type Sketch

A Task wraps an async coroutine. It supports co_await for chaining.

template <typename T>
struct Task {
    struct promise_type;
    std::coroutine_handle<promise_type> handle;
    // ...
};

co_await Mechanics

When the compiler sees co_await expr, it expects expr to be an awaitable. An awaitable has three methods:

  • await_ready() — false to suspend, true to skip
  • await_suspend(handle) — what to do while suspended
  • await_resume() — value to give to the calling code

Awaitable Example: Sleep

An awaitable that schedules a resume after a delay.

struct Sleep {
    std::chrono::milliseconds duration;
    bool await_ready() const { return false; }
    void await_suspend(std::coroutine_handle<> h) {
        std::thread([h, this] {
            std::this_thread::sleep_for(duration);
            h.resume();
        }).detach();
    }
    void await_resume() {}
};

Task<void> example() {
    co_await Sleep{500ms};
    std::cout << "after 500ms";
}

Awaitable for HTTP

Library-provided awaitables for HTTP requests, timers, file I/O. Compose them like synchronous calls.

Task<std::string> fetch() {
    auto data = co_await http_get("/api/data");
    co_return data;
}

Composing Tasks

Awaiting one task inside another chains them. The outer task suspends until the inner finishes.

Task<int> outer() {
    int x = co_await inner_a();
    int y = co_await inner_b();
    co_return x + y;
}

Parallel Tasks

For parallel async work, start tasks without awaiting each one immediately, then await them all.

Task<int> sum_tasks() {
    auto a = inner_a();
    auto b = inner_b();
    int x = co_await a;
    int y = co_await b;
    co_return x + y;
}

Exception Propagation

Exceptions thrown inside an async task can be captured in the promise s unhandled_exception and rethrown on the awaiter side.

Cancellation

The standard library does not provide a cancellation primitive yet. Libraries (cppcoro, boost.cobalt) build their own — usually via cancellation tokens or stop sources.

Boost.Asio Integration

Asio supports coroutines natively. co_await on async_read, async_write, and similar operations. The cleanest C++ async I/O today.

Performance

Coroutine resumption is much cheaper than thread switching. The compiler often inlines parts of the state machine. Profile to verify your stack of awaitables compiles cleanly.

Future Directions

C++23 added std::generator and improvements. C++26 may bring std::execution (sender/receiver) — a more general async framework that interoperates with coroutines.

Quick Check

Which three methods does an awaitable type need?

Recap

Async tasks combine co_await with awaitables to write callback-free async code. Awaitables have await_ready, await_suspend, and await_resume. Libraries (cppcoro, Asio) provide ready-made task and awaitable types.

Frequently asked questions

Is the “Async Tasks and Awaiter Types” lesson free?

Yes — the full text of “Async Tasks and Awaiter Types” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Async Tasks and Awaiter Types”?

Compose async tasks with custom awaiter and promise types. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Async Tasks and Awaiter Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Coroutine Concepts co_await co_yield co_return
  2. Implementing a Simple Generator
  3. Async Tasks and Awaiter Types
  4. Coroutine Frame Allocation
← Back to C++ Academy