Skip to content

Commit

Permalink
Process payload synchronously in debug mode
Browse files Browse the repository at this point in the history
Closes amphp#7.
  • Loading branch information
theofidry committed Mar 1, 2018
1 parent 5e11830 commit 758f6fa
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ function parallel(callable $callable, Pool $pool = null): callable {
*/
function parallelMap(array $array, callable $callable, Pool $pool = null): Promise {
return call(function () use ($array, $callable, $pool) {
// Amp\Promise\any() guarantees that all operations finished prior to resolving. Amp\Promise\all() doesn't.
// Additionally, we return all errors as a MultiReasonException instead of throwing on the first error.
list($errors, $results) = yield any(\array_map(parallel($callable, $pool), $array));
$env = \getenv("AMP_DEBUG");

if ((false === $env || $env === "0" && $env === "false") || (\defined("AMP_DEBUG") && false === \AMP_DEBUG)) {
// Amp\Promise\any() guarantees that all operations finished prior to resolving. Amp\Promise\all() doesn't.
// Additionally, we return all errors as a MultiReasonException instead of throwing on the first error.
list($errors, $results) = yield any(\array_map(parallel($callable, $pool), $array));
} else {
list($errors, $results) = yield any(\array_map($callable, $array));
}

if ($errors) {
throw new MultiReasonException($errors);
Expand Down

0 comments on commit 758f6fa

Please sign in to comment.