title: How to get execute query asynchronously and process them in correct order with Guzzle?
body: <p>I am using the Guzzle version 6, and I want to send all my request in asynchronously in parallel and do some work on the response in the same order as the queries. </p>
  
  <p>For example, if I query url-1 to url-8, I want to asynchronously let the first four queries (url-1 to url-4) to be resolved in any order, and then process the url-response-body-1 to url-response-body-4 in the right ascending order. Then the next four queries (url-5 to url-8) would be resolved also asynchronously and so on...</p>
  
  <p>I know that I could in any way store all the responses of my queries in memory and then work on them when all the queries are made but I want to be able a small memory footprint and low latency code.</p>
  
  <p>Here the code I am using so far :</p>
  
  <pre><code>$url_arr = array("/home_search.php?q=criteria1","/home_search.php?q=criteria2","/home_search.php?q=criteria3","/home_search.php?q=criteria4","/home_search.php?q=criteria5","/home_search.php?q=criteria6");
  
  $promises = (function () use ($url_arr,$client) {
    foreach ($url_arr as $uri) {
      // don't forget using generator
      yield  $client-&gt;getAsync( $uri);        
    }
  })();
  
  $eachPromise = new EachPromise($promises, [
    // how many concurrency we are use
    'concurrency' =&gt; 4,
    'fulfilled' =&gt; function (Response $response) {
       doMyWork($response-&gt;getBody());
      },
    'rejected' =&gt; function ($reason) {
      // handle promise rejected here
    }
  ]);
  $eachPromise-&gt;promise()-&gt;wait();
  </code></pre>
  
  <p>The problem is that the function doMyWork receives the response in any order, for example, it sometimes receives the response of the second url get query before the first one.
  Any help will be very much appreciated.</p>