Future A
Future B
Future C
operationA()
operationB()
combinator()
ResponseA
ResponseB
ResponseAny
getHttpUrl(serviceA)
getHttpUrl(serviceB)
when_any()
DiscoveryResponse
DiscoveredServiceResponse
getHttpUrl(discoveryService)
getHttpUrl(discoveredUrl)
asyncly::Future<Response> getHttpUrl(const std::string& url);
void run() {
getHttpUrl("https://discovery-service.com/discover-some-service")
.then(
[](Response responseFromDiscoveryService) {
// note that we return another future
return getHttpUrl(responseFromDiscoveryService.body);
})
.then(
[](Response response) {
// do something with the response here
std::cout << response.body << std::endl;
});
}
DiscoveryResponse1
DiscoveredServiceResponse1
getHttpUrl(discoveryService)
getHttpUrl(discoveredUrl1 + "/1")
DiscoveryResponse2
DiscoveredServiceResponse2
getHttpUrl(discoveredUrl + "/2")
DiscoveryResponse
asyncly::split()
asyncly::Future<Response> getHttpUrl(const std::string& url);
void run() {
auto serviceUrlFuture =
getHttpUrl("https://discovery.com/some-service");
// split consumes one future and returns a tuple of two futures
// T must be copyable!
auto serviceUrlPair = asyncly::split(std::move(serviceUrlFuture));
auto endpointAFuture = std::get<0>(serviceUrlPair)
.then([](auto serviceUrl) {
return getHttpUrl(serviceUrl.body + "/1");
});
auto endpointBFuture = std::get<1>(serviceUrlPair)
.then([](auto serviceUrl) {
return getHttpUrl(serviceUrl.body + "/2");
});
// proceed with doing something with the two responses
}
ResponseA
ResponseB
ResponseAorB
getHttpUrl(serviceA)
getHttpUrl(serviceB)
when_any()
asyncly::Future<Response> getHttpUrl(const std::string& url);
void run() {
auto responseFutureA = getHttpUrl("https://service-a");
auto responseFutureB = getHttpUrl("https://service-b");
asyncly::when_any(
std::move(responseFutureA),
std::move(responseFutureB))
.then([](Response result) {
std::cout << result.body << std::endl;
});
}
ResponseA
ResponseB
ResponseAandB
getHttpUrl(serviceA)
getHttpUrl(serviceB)
when_all()
asyncly::Future<Response> getHttpUrl(const std::string& url);
void run() {
auto responseFutureA = getHttpUrl("https://service-a");
auto responseFutureB = getHttpUrl("https://service-b");
asyncly::when_all(
std::move(responseFutureA),
std::move(responseFutureB))
.then([](Response responseA, Response responseB) {
std::cout << responseA.body << std::endl;
std::cout << responseB.body << std::endl;
});
}
DiscoveryResponse1
DiscoveredServiceResponse1
getHttpUrl(discoveryService)
getHttpUrl(discoveredUrl1 + "/1")
DiscoveryResponse2
DiscoveredServiceResponse2
getHttpUrl(discoveredUrl + "/2")
DiscoveryResponse
asyncly::split()
asyncly::when_all()
BothResponses
asyncly::split()
asyncly::add_timeout()
ResponsesOrTimeout