asio C++ library

PrevUpHomeNext

experimental::promise

A disposable handle for an eager operation.

template<
    typename Signature = void(),
    typename Executor = asio::any_io_executor,
    typename Allocator = std::allocator<void>>
struct promise
Member Functions

Name

Description

cancel

Cancel the promise. Usually done through the destructor.

completed

Check if the promise is completed already.

operator()

Wait for the promise to become ready.

promise [constructor]

~promise [destructor]

Destruct the promise and cancel the operation.

Signature The signature of the operation.

Executor The executor to be used by the promise (taken from the operation).

Allocator The allocator used for the promise. Can be set through use_allocator.

A promise can be used to initiate an asynchronous option that can be completed later. If the promise gets destroyed before completion, the operation gets a cancel signal and the result is ignored.

A promise fulfills the requirements of async_operation.

Examples

Reading and writing from one coroutine.

awaitable<void> read_write_some(asio::ip::tcp::socket & sock,
    asio::mutable_buffer read_buf, asio::const_buffer to_write)
{
  auto p = asio::async_read(read_buf, asio::use_awaitable);
  co_await asio::async_write_some(to_write, asio::deferred);
  co_await p;
}
Requirements

Header: asio/experimental/promise.hpp

Convenience header: None


PrevUpHomeNext