In CAF, response promises allow an actor to send and receive other messages prior to replying to a particular request. My issue is how to use the result of that promise.
// function-based, statically typed, event-based API
adder::behavior_type calculator_master(adder::pointer self) {
auto w = self->spawn(worker);
return {
[=](add_atom x, int y, int z) -> result<int> {
auto rp = self->make_response_promise<int>();
self->request(w, infinite, x, y, z).then([=](int result) mutable {
rp.deliver(result);
});
return rp;
}
}; }
I basically have to use the value of rp before the return statement. Please let me know if you have any ideas
I just share some of my understanding.
Response promise is not exactly the same as future/promise (e.g. std::future and std::promise), so you cannot use response promise as you use future/promise.
One usage of response promise is to reply something to the sender while the task is not yet done. Supposed actor
Asends a request to actorBbutBis not the one that handles the request.Bdelivers the request to another actorCandChandles the request. In this case,Bforwards the request toCand has to reply something toA.Bcannot be blocked and wait forC, otherwiseBcannot handle new requests. At the end,Bsends a response promise toA. The response promise created byBis actually used byAso thatAcan wait for the result ofC.P.S. Sad that I don't have enough reputation to make a comment.