CAF Promise Result

172 views Asked by At

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

1

There are 1 answers

0
Zhao Yunjian On

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 actor B but B is not the one that handles the request. B delivers the request to another actor C and C handles the request. In this case, B forwards the request to C and has to reply something to A. B cannot be blocked and wait for C, otherwise B cannot handle new requests. At the end, B sends a response promise to A. The response promise created by B is actually used by A so that A can wait for the result of C.

P.S. Sad that I don't have enough reputation to make a comment.