template<typename... Result>
cti::promisify class

Helper class for converting callback taking callable types into a a continuable. Various styles are supported.

Template parameters
Result The result of the converted continuable, this should align with the arguments that are passed to the callback.

Contents

  • from: Converts callback taking callable types into continuables which pass an error code as first parameter and the rest of the result afterwards.

Public static functions

template<typename Callable, typename... Args>
static auto from(Callable&& callable, Args && ... args) -> auto
Converts callback taking callable types into a continuable. This applies to calls which pass an error code as first parameter and the rest of the asynchronous result afterwards.
template<typename Resolver, typename Callable, typename... Args>
static auto with(Resolver&& resolver, Callable&& callable, Args && ... args) -> auto
Converts callback taking callable types into a continuable. This applies to calls which pass an error code as first parameter and the rest of the asynchronous result afterwards.

Function documentation

template<typename... Result> template<typename Callable, typename... Args>
static auto cti::promisify<Result>::from(Callable&& callable, Args && ... args)

Converts callback taking callable types into a continuable. This applies to calls which pass an error code as first parameter and the rest of the asynchronous result afterwards.

See an example of how to promisify boost asio's async_resolve below:

auto async_resolve(std::string host, std::string service) {
  return cti::promisify<asio::ip::udp::resolver::iterator>::from(
      [&](auto&&... args) {
        resolver_.async_resolve(std::forward<decltype(args)>(args)...);
      },
      std::move(host), std::move(service));
}

A given error variable is converted to the used error type. If this isn't possible you need to create a custom resolver callable object

template<typename... Result> template<typename Resolver, typename Callable, typename... Args>
static auto cti::promisify<Result>::with(Resolver&& resolver, Callable&& callable, Args && ... args)

Converts callback taking callable types into a continuable. This applies to calls which pass an error code as first parameter and the rest of the asynchronous result afterwards.

This modification of from additionally takes a resolver callable object which is used to resolve the promise from the given result.

See an example of how to promisify boost asio's async_resolve below:

auto async_resolve(std::string host, std::string service) {
  return cti::promisify<asio::ip::udp::resolver::iterator>::with(
      [](auto&& promise, auto&& e, auto&&... args) {
        if (e) {
          promise.set_exception(std::forward<decltype(e)>(e));
        } else {
          promise.set_value(std::forward<decltype(args)>(args)...);
        }
      },
      [&](auto&&... args) {
        resolver_.async_resolve(std::forward<decltype(args)>(args)...);
      },
      std::move(host), std::move(service));
}