Repository: laravel/framework
File: src/Illuminate/Http/Client/PendingRequest.php (PHP)

Code before:
     * @param  non-negative-int|null  $concurrency
     * @return array<array-key, \Illuminate\Http\Client\Response|\Illuminate\Http\Client\ConnectionException|\Illuminate\Http\Client\RequestException>
     */
    public function pool(callable $callback, ?int $concurrency = 0)
    {
        $results = [];

        $requests = tap(new Pool($this->factory), $callback)->getRequests();

        if ($concurrency === null) {
            (new Collection($requests))->each(function ($item) {
                if ($item instanceof static) {
                    $item = $item->getPromise();
                }
                if ($item instanceof LazyPromise) {
                    $item->buildPromise();
                }
            });
            foreach ($requests as $key => $item) {
                $results[$key] = $item instanceof static ? $item->getPromise()->wait() : $item->wait();
            }

            return $results;
        }

        $concurrency = $concurrency === 0 ? count($requests) : $concurrency;

        (new Collection($requests))->chunk($concurrency)
            ->each(static function (Collection $requests) use ($concurrency, &$results) {
                $promises = [];
                foreach ($requests as $key => $item) {
                    $promise = $item instanceof static ? $item->getPromise() : $item;
                    $promises[$key] = $promise instanceof LazyPromise ? $promise->buildPromise() : $promise;
                }

                (new EachPromise($promises, [
                    'fulfilled' => function ($result, $key) use (&$results) {
                        $results[$key] = $result;
                    },
                    'rejected' => function ($reason, $key) use (&$results) {
                        $results[$key] = $reason;
                    },
                    'concurrency' => $concurrency,
                ]))->promise()->wait();
            });

        return $results;
    }

    /**
     * Send a pool of asynchronous requests concurrently, with callbacks for introspection.

Review comment (suggestion):
Added this for backwards compatibility: so `null` executes everything at once as it always had, throwing an exception if the user marked `throws()` or there's a connection timeout.

I would like to see this entirely removed in Laravel 13.

To me, it makes no sense in the context of request pooling, and it changes the underlying behavior. Here, we throw an exception on a connection timeout, whereas in any other case, we return the ConnectionException as a part of the array.

Code after:
     * @param  non-negative-int|null  $concurrency
     * @return array<array-key, \Illuminate\Http\Client\Response|\Illuminate\Http\Client\ConnectionException|\Illuminate\Http\Client\RequestException>
     */
    public function pool(callable $callback, ?int $concurrency = null)
    {
        $results = [];

        $requests = tap(new Pool($this->factory), $callback)->getRequests();

        if ($concurrency === null) {
            (new Collection($requests))->each(static function ($item) {
                if ($item instanceof static) {
                    $item = $item->getPromise();
                }

                if ($item instanceof LazyPromise) {
                    $item->buildPromise();
                }
            });

            foreach ($requests as $key => $item) {
                $results[$key] = $item instanceof static ? $item->getPromise()->wait() : $item->wait();
            }

            return $results;
        }

        $concurrency = $concurrency === 0 ? count($requests) : $concurrency;

        $promiseGenerator = static function () use ($requests) {
            foreach ($requests as $key => $item) {
                $promise = $item instanceof static ? $item->getPromise() : $item;
                yield $key => $promise instanceof LazyPromise ? $promise->buildPromise() : $promise;
            }
        };

        (new EachPromise($promiseGenerator(), [
            'fulfilled' => function ($result, $key) use (&$results) {
                $results[$key] = $result;
            },
            'rejected' => function ($reason, $key) use (&$results) {
                $results[$key] = $reason;
            },
            'concurrency' => $concurrency,
        ]))->promise()->wait();

        return $results;
    }

    /**
     * Send a pool of asynchronous requests concurrently, with callbacks for introspection.