How to handle async request concurrency with Promise in Guzzle 6

Ardani Rohman
Jun 17, 2019 · 3 min read

A. Quick start

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.check here for full documentation http://docs.guzzlephp.org/en/stable/index.html

A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled

how the concurrency work

We are all know this library. I loved Guzzle because this make simple the request of data from Api. I not explain a basic request but i will explain how to request multiple API concurrently using async request and make us saving a lot of time. let go with basic a Promise in Guzzle.

find more example here https://github.com/guzzle/promises

B. Async Request with Promise

below is example basic async request with promise

pay attention for line 8, key array of promise is how to get the results after wait() executed. if we don’t set the key the response result can be random or same order as the request promises are added to the promises array, depend the function promise what we use.

another way we can use Promise\all, Promise\each to get result promise using iteration keeping scroll to get an example 🙂

not only using get but we can call async request with other method

this is some function of promise

  1. unwrap : waits on all of the provided promises and returns the fulfilled values. Returns an array that contains the value of each promise (in the same order the promises were provided). An exception is thrown if any of the promises are rejected.
  2. all : given an array of promises, return a promise that is fulfilled when all the items in the array are fulfilled. The promise’s fulfillment value is an array with fulfillment values at respective positions to the original array. If any promise in the array rejects, the returned promise is rejected with the rejection reason.
  3. each : given an iterator that yields promises or values, returns a promise that is fulfilled with a null value when the iterator has been consumed or the aggregate promise has been fulfilled or rejected.
  4. settle : returns a promise that is fulfilled when all of the provided promises have been fulfilled or rejected. The returned promise is fulfilled with an array of inspection state arrays.

The benefit of all is that the responses are returned in the same order as the request promises are added to the promises array.

C. Concurrency in Promise

after we learn about basic async next we can use concurrency means multiple computations are happening at the same time that is good when we deal with a lot of request with the same time. For concurrency we must use EachPromise Class and Yield generator, don’t forget wait() in end of code.

D. Bonus : improve with cache

The responses could be cached to limit the total data sent over HTTP but we are still can use cache to handle it 💪.

E. Conclusion

The promise with concurrency help us to faster for process execution time. one thing should you know is when we use promise is make little hard to test because they are running to promise, when i use the promise first todo is check process request is run well in synchronous mode after that you can change request to asynchronous.

if you enjoy about this article please follow me and give me some claps 😄. thanks keep sharing!.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade