You are here

function some in Lockr 7.3

Initiate a competitive race between multiple promises or values (values will become immediately fulfilled promises).

When count amount of promises have been fulfilled, the returned promise is fulfilled with an array that contains the fulfillment values of the winners in order of resolution.

This prommise is rejected with a {if the number of fulfilled promises is less than the desired $count.

Parameters

int $count Total number of promises.:

mixed $promises Promises or values.:

Return value

PromiseInterface

See also

GuzzleHttp\Promise\AggregateException}

File

vendor/guzzlehttp/promises/src/functions.php, line 252

Namespace

GuzzleHttp\Promise

Code

function some($count, $promises) {
  $results = [];
  $rejections = [];
  return each($promises, function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
    if ($p
      ->getState() !== PromiseInterface::PENDING) {
      return;
    }
    $results[$idx] = $value;
    if (count($results) >= $count) {
      $p
        ->resolve(null);
    }
  }, function ($reason) use (&$rejections) {
    $rejections[] = $reason;
  })
    ->then(function () use (&$results, &$rejections, $count) {
    if (count($results) !== $count) {
      throw new AggregateException('Not enough promises to fulfill count', $rejections);
    }
    ksort($results);
    return array_values($results);
  });
}