Skip to content

Waiting for responses

Greg Bowler edited this page Mar 16, 2026 · 2 revisions

Once requests have been queued, we need to decide how we want execution to continue. This library offers three main ways to do that:

  • wait()
  • all()
  • awaitFetch()

wait()

wait() runs the internal event loop until all queued requests are complete.

$http->fetch("https://example.com/a");
$http->fetch("https://example.com/b");

$http->wait();

This is the most direct and most commonly used option.

all()

all() is similar to wait(), but returns a Promise that resolves when the final request has completed.

$http->fetch("https://example.com/a");
$http->fetch("https://example.com/b");

$http->all()->then(function(float $seconds) {
	echo "All requests completed in $seconds seconds.", PHP_EOL;
});

This is helpful when we want the completion of the whole batch to become part of the promise chain itself.

awaitFetch()

Sometimes we do not want to think in terms of chained promises at all. For that case there is awaitFetch(), which blocks until the response object is available and returns it directly.

$response = $http->awaitFetch("https://example.com/api");
echo $response->status, PHP_EOL;

Once we have the response, we can continue in the same blocking style:

$response = $http->awaitFetch("https://example.com/data.json");
$json = $response->awaitJson();

Which should we use?

As a rule of thumb:

  • use fetch() with wait() when we want concurrent requests and promise chains
  • use all() when we want one promise for the completion of the whole batch
  • use awaitFetch() when a blocking style is clearer

None of these approaches is more "correct" than the others. It is mostly about choosing the clearest expression for the job at hand.


The next section looks specifically at concurrent requests.

Clone this wiki locally