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
A
sends a request to actorB
butB
is not the one that handles the request.B
delivers the request to another actorC
andC
handles the request. In this case,B
forwards the request toC
and has to reply something toA
.B
cannot be blocked and wait forC
, otherwiseB
cannot handle new requests. At the end,B
sends a response promise toA
. The response promise created byB
is actually used byA
so thatA
can wait for the result ofC
.P.S. Sad that I don't have enough reputation to make a comment.