Creating continuables
Explains how to create a continuable_
A continuable is an arbitrary instantiation of a continuable_
The continuable_
From a value or exception
The library provides make_
auto one = cti::make_ready_continuable(0); cti::continuable<int, float, char> three = cti::make_ready_continuable(0, 1.f, '2');
Additionally a continuable_
cti::continuable<int> c = cti::make_exceptional_continuable(std::exception{});
From a promise taking callable
The main function for creating a continuable_std::promise
which is created by the user.
The promise_http_request
function which resolves the request asynchronously trough a std::string
.
auto http_request(std::string url) { return cti::make_continuable<std::string>( [url = std::move(url)](auto&& promise) { // Resolve the promise upon completion of the task. promise.set_value("<html> ... </html>"); // Or promise.set_exception(...); }); }
An alternative would be a continuable_
auto wait_for(std::chrono::milliseconds duration) { return cti::make_continuable<void>([](auto&& promise) { // ^^^^ // Resolve the promise later when the duration is over promise.set_value(); });
A continuable_
auto resolve_sth() { return cti::make_continuable<int, int, float, char>( [](auto&& promise) { promise.set_value(0, 1, 2.f, '3'); });
A promise_
The continuable invocation model
An asynchronous call hierarchy that is stored inside the continuable_std::future
which execute the asynchronous call hierarchy instantly on creation (eager evaluation).
The lazy evaluation strategy used by continuables has many benefits over eager evaluation that is used by other common implementations:
- prevention of side effects
- evasion of race conditions
- ensured deterministic behaviour.
The asynchronous call hierarchy is started when the continuable_