avatarArdani Rohman

Summary

The web content provides a guide on using Guzzle 6 to handle asynchronous HTTP requests concurrently with Promises, offering techniques for efficient API request batching, concurrency management, and performance optimization through caching.

Abstract

The article titled "How to handle async request concurrency with Promise in Guzzle 6" serves as a comprehensive tutorial for PHP developers looking to optimize their HTTP requests. It introduces Guzzle, a PHP HTTP client, and explains the concept of Promises for managing asynchronous operations. The author emphasizes the time-saving benefits of concurrently requesting multiple APIs and demonstrates how to use Guzzle's Promise functionality with examples. The article covers the basics of async requests with Promises, the use of Promise\all and Promise\each for handling iterative promise results, and the advantages of maintaining response order. It also delves into advanced topics such as using the EachPromise class and Yield generator for concurrency, and suggests caching responses to reduce HTTP data transfer. The conclusion highlights the efficiency gains from using Promises with concurrency and advises testing synchronously before switching to asynchronous requests.

Opinions

  • The author expresses a preference for Guzzle due to its simplicity in making API data requests.
  • They highlight the importance of using keys when retrieving results after a wait() call to ensure the correct order of responses.
  • The author suggests that the all method is beneficial because it returns responses in the order that the promises were added, regardless of which promise resolves first.
  • They advocate for the use of caching to improve performance by limiting the amount of data sent over HTTP.
  • The author recommends testing request processes synchronously before implementing asynchronous operations to ensure proper functionality.
  • They encourage readers to follow them and show appreciation for the article through claps, indicating a desire for community engagement and feedback.
  • The author endorses an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), suggesting it offers similar performance and features.

How to handle async request concurrency with Promise in Guzzle 6

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.

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!.

PHP
Laravel
Asynchronous
Concurrency
API
Recommended from ReadMedium