Skip to content

Instantly share code, notes, and snippets.

@jsor
Created December 18, 2013 09:04
Show Gist options
  • Save jsor/8019388 to your computer and use it in GitHub Desktop.
Save jsor/8019388 to your computer and use it in GitHub Desktop.
React\Promise\settle()
<?php
namespace React\Promise;
function settle($promisesOrValues)
{
return resolve($promisesOrValues)
->then(function ($array) {
if (!is_array($array) || !$array) {
return resolve([]);
}
return new Promise(function ($resolve, $reject, $progress) use ($array) {
$toResolve = count($array);
$values = [];
$toFulfilledState = function ($x) {
return ['state' => 'fulfilled', 'value' => $x];
};
$toRejectedState = function ($x) {
return ['state' => 'rejected', 'reason' => $x];
};
foreach ($array as $i => $promiseOrValue) {
resolve($promiseOrValue)
->then($toFulfilledState, $toRejectedState)
->then(
function ($mapped) use ($i, &$values, &$toResolve, $resolve) {
$values[$i] = $mapped;
if (0 === --$toResolve) {
$resolve($values);
}
},
$reject,
$progress
);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment