Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

experimental::promise

A disposable handle for an eager operation.

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

Name

Description

value_type

The value that's returned by the 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(boost::asio::ip::tcp::socket & sock,
    boost::asio::mutable_buffer read_buf, boost::asio::const_buffer to_write)
{
  auto p = boost::asio::async_read(read_buf, boost::asio::use_awaitable);
  co_await boost::asio::async_write_some(to_write, boost::asio::deferred);
  co_await p;
}
Requirements

Header: boost/asio/experimental/promise.hpp

Convenience header: None


PrevUpHomeNext