Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with promises #145

Closed
dropdevcoding opened this issue Dec 9, 2016 · 2 comments
Closed

How to deal with promises #145

dropdevcoding opened this issue Dec 9, 2016 · 2 comments

Comments

@dropdevcoding
Copy link

Hello,
since I'm implementing an application using ES/CQRS (completely using Prooph components), I'm wondering how to deal with the following situation:

  1. I'm sending a GET request to the server (NOT react PHP) and I want to return a result from my read model using the QueryBus
  2. The QueryBus works asynchronously which is perfect since the read model could be stored anywhere
  3. My Finder returns the promise
  4. Now, since I'm on a blocking server using a regular webframework my action wants me to return a value directly (synchronously)

So, how to deal with it?

public function myAction($request) {
    $query = MyQuery::with($request->getTheParam());
    $promise = $this->queryBus->dispatch($query);
    $result = null;
    $promise->then(function ($queryResult) use (&$result) {
       $result = $queryResult;
   });

  return new ActionResult($result);
}

This approach works since the query happens within the request on the same server accessing the DB and blocks but it won't work anymore when moving my read model to any other async service because the action would return without waiting for the promise to be finished (of course).

In addition my current solution above feels really clumsy imho. I'm I missing something / getting something wrong?

Guzzle Promises support $promise->wait() which would be a solution for this case I think.

Thank you in advance!

@dropdevcoding dropdevcoding changed the title How to deal with Promises How to deal with promises Dec 9, 2016
@codeliner
Copy link
Member

Hi, we just return a reactPHP promise to allow you to execute queries async and continue with other stuff while you are waiting for the response. How to deal with it is userland implementation ;)
Maybe you can get some ideas from this post: http://stephencoakley.com/2015/06/11/integrating-guzzle-6-asynchronous-requests-with-reactphp

Let me know if it helps you!

@dropdevcoding
Copy link
Author

Looks promising ;-)

Also I realized that you are using the approach I mentioned above as well in your PSR-7 middleware what already soothed me sufficiently.

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants