Code Monkey home page Code Monkey logo

Comments (9)

csarrazi avatar csarrazi commented on June 14, 2024

Well, the clients created by the bundle are actually Guzzle clients. So if Guzzle supports something, this bundles supports it too.

from csaguzzlebundle.

csarrazi avatar csarrazi commented on June 14, 2024

FYI, the client class used by the bundle is GuzzleHttp\Client, so you should be able to use sendAsync, or any *Async method.

from csaguzzlebundle.

thim81 avatar thim81 commented on June 14, 2024

It seems something is missing or not working with regards to the Async call.

$client = $this->get('csa_guzzle.client.api_test');
$promise = $client->postAsync('/api/push/user', ['json' => $msg]);

$promise->then(
                  function (ResponseInterface $res) {
                      var_dump( $res->getStatusCode());
                  },
                  function (RequestException $e) {
                      var_dump($e->getMessage() );
                      var_dump($e->getRequest()->getMethod());
                  }

But nothing get printed.

So it seems that the function is called and Guzzle responds but nothing happens.

from csaguzzlebundle.

csarrazi avatar csarrazi commented on June 14, 2024

You need to unwrap your promise by using the wait() method.

$reponse = $promise->wait();

If you need to unwrap multiple methods, you can use GuzzleHttp\Promise\unwrap($promises), where $promises is an array of promises.

from csaguzzlebundle.

csarrazi avatar csarrazi commented on June 14, 2024

Keep in mind that PHP is not async. Your promise won't be automatically be resolved unless you call the wait() method of the promise. And in this case, it will block until the promise is resolved.

from csaguzzlebundle.

csarrazi avatar csarrazi commented on June 14, 2024

If you need to do true async, then you should work with asynchronous workers, or by launching background processes and waiting on them.

Or have logic inside the then() method of your promise.

from csaguzzlebundle.

csarrazi avatar csarrazi commented on June 14, 2024

Please read the Guzzle documentation on this: http://guzzle.readthedocs.org/en/latest/quickstart.html#concurrent-requests

from csaguzzlebundle.

thim81 avatar thim81 commented on June 14, 2024

I have read the documentation about the async calls and my impression was that you can fire async calls and let php continue without the wait.

Because I want to push data to external API without the current function have to wait for the result, make the whole function slower and I had the impression that this is exactly what the *Async functions support.
http://docs.guzzlephp.org/en/latest/faq.html#can-guzzle-send-asynchronous-requests

from csaguzzlebundle.

thim81 avatar thim81 commented on June 14, 2024

To close this issue, your feedback was again helpful.
Adding $promise->wait(); caused the async call to fire properly and the API call was made

$client = $this->get('csa_guzzle.client.api_test');
$promise = $client->postAsync('/api/push/user', ['json' => $msg]);
$reponse = $promise->wait();

from csaguzzlebundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.