Created
December 18, 2013 09:04
-
-
Save jsor/8019388 to your computer and use it in GitHub Desktop.
React\Promise\settle()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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