From 1bfd55921a062777b611a209c2612d9a43abf822 Mon Sep 17 00:00:00 2001 From: Mathieu Ledru Date: Mon, 19 Apr 2021 16:59:02 +0200 Subject: [PATCH] :art: not using channels way --- src/Adapter/Swoole/Internal/SwoolePromise.php | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Adapter/Swoole/Internal/SwoolePromise.php b/src/Adapter/Swoole/Internal/SwoolePromise.php index cf24b53..bbeb2b1 100644 --- a/src/Adapter/Swoole/Internal/SwoolePromise.php +++ b/src/Adapter/Swoole/Internal/SwoolePromise.php @@ -180,39 +180,45 @@ public static function all(iterable $promises): SwoolePromise $ticks = count($promises); $firstError = null; - $channel = new Channel($ticks); + //$channel = new Channel($ticks); $result = []; $key = 0; foreach ($promises as $promise) { if (!$promise instanceof SwoolePromise) { - $channel->close(); + //$channel->close(); throw new RuntimeException( 'Supported only SwoolePromise instance' ); } - $promise->then(function ($value) use ($key, &$result, $channel) { + $promise->then(function ($value) use ($key, &$result, &$ticks, $resolve/* $channel*/) { $result[$key] = $value; - $channel->push(true); + $ticks--; + //$channel->push(true); + if($ticks === 0) { + $resolve($result); + } return $value; - }, function ($error) use ($channel, &$firstError) { - $channel->push(true); + }, function ($error) use (/*$channel, */&$firstError, &$ticks, $reject) { + //$channel->push(true); + $ticks--; if ($firstError === null) { $firstError = $error; + $reject($firstError); } }); $key++; } - while ($ticks--) { - $channel->pop(); - } - $channel->close(); + //while ($ticks--) { + // $channel->pop(); + //} + //$channel->close(); - if ($firstError !== null) { + /*if ($firstError !== null) { $reject($firstError); return; } - $resolve($result); + $resolve($result);*/ }); }