Documentation Packages Support

Amp ⋅ Promise Combinators

Multiple promises can be combined into a single promise using different functions.


Amp\Promise\all() combines an array of promise objects into a single promise that will resolve when all promises in the group resolve. If any one of the Amp\Promise instances fails the combinator’s Promise will fail. Otherwise the resulting Promise succeeds with an array matching keys from the input array to their resolved values.

The all() combinator is extremely powerful because it allows us to concurrently execute many asynchronous operations at the same time. Let’s look at a simple example using the Amp HTTP client (Artax) to retrieve multiple HTTP resources concurrently:


use Amp\Loop;
use Amp\Promise;

Loop::run(function () {
    $httpClient = new Amp\Artax\DefaultClient;
    $uris = [
        "google" => "",
        "news"   => "",
        "bing"   => "",
        "yahoo"  => "",

    try {
        // magic combinator sauce to flatten the promise
        // array into a single promise.
        // yielding an array is an implicit "yield Amp\Promise\all($array)".
        $responses = yield array_map(function ($uri) use ($httpClient) {
            return $httpClient->request($uri);
        }, $uris);

        foreach ($responses as $key => $response) {
                "%s | HTTP/%s %d %s\n",
    } catch (Amp\MultiReasonException $e) {
        // If any one of the requests fails the combo will fail and
        // be thrown back into our generator.
        echo $e->getMessage(), "\n";



Amp\Promise\some() is the same as all() except that it tolerates individual failures. As long as at least one promise in the passed succeeds, the combined promise will succeed. The successful resolution value is an array of the form [$arrayOfErrors, $arrayOfValues]. The individual keys in the component arrays are preserved from the promise array passed to the functor for evaluation.


Amp\Promise\any() is the same as some() except that it tolerates all failures. It will succeed even if all promises failed.


Amp\Promise\first() resolves with the first successful result. The resulting promise will only fail if all promises in the group fail or if the promise array is empty.