1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

Rename any() -> some(), change any() to never fail

This commit is contained in:
Daniel Lowrey 2014-10-22 18:14:43 -04:00
parent f1f967e0ce
commit 7b11a8e0c3
2 changed files with 48 additions and 0 deletions

View File

@ -105,6 +105,41 @@ class Combinator {
return $future->promise();
}
/**
* Resolves with a two-item array delineating successful and failed Promise results.
*
* This method is exactly the same as some() except it will *never* fail.
*
* @param array $promises
* @return \Amp\Promise
*/
public function any(array $promises) {
if (empty($promises)) {
return new Success([], []);
}
$results = $errors = [];
$count = count($promises);
$future = new Future($this->reactor);
foreach ($promises as $key => $promise) {
$promise = ($promise instanceof Promise) ? $promise : new Success($promise);
$promise->when(function($error, $result) use (&$count, &$results, &$errors, $key, $future) {
if ($error) {
$errors[$key] = $error;
} else {
$results[$key] = $result;
}
if ($count-- === 1) {
$future->succeed([$errors, $results]);
}
});
}
return $future->promise();
}
/**
* Resolves with the first successful Promise value. The resulting Promise will only fail if all
* Promise values in the group fail or if the initial Promise array is empty.

View File

@ -286,6 +286,19 @@ function some(array $promises) {
return combinator()->some($promises);
}
/**
* Resolves with a two-item array delineating successful and failed Promise results.
*
* This function is the same as some() with the notable exception that it will never fail even
* if all promises in the array resolve unsuccessfully.
*
* @param array $promises
* @return \Amp\Promise
*/
function any(array $promises) {
return combinator()->any($promises);
}
/**
* Resolves with the first successful Promise value. The resulting Promise will only fail if all
* Promise values in the group fail or if the initial Promise array is empty.