Skip to content

Conversation

@cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Nov 28, 2025

Guzzle Promises are immutable, which means userland chaining isn't possible since we don't have access to the underlying array.

For example, this will not work to return the string length of each request. It will instead just return the responses.

$response = Http::pool(function (Pool $pool) {
    $pool->as('cosmatech')
        ->get('https://cosmastech.com')
        ->then(
            fn ($response) => strlen($response->body())
        );
    $pool->as('laravel')
        ->get('https://laravel.com')
        ->then(
            fn ($response) => strlen($response->body())
        );
});

dd($response);
/*
array:2 [
  "cosmatech" => Illuminate\Http\Client\Response {
   ...
   },
   "laravel" => Illuminate\Http\Client\Response {
   ...
   }
]
*/

Were this PR to be accepted, then the above can be converted to:

$response = Http::pool(function (Pool $pool) {
    $pool->as('cosmatech', fn (PendingRequest $pr) =>
        $pr->get('https://cosmastech.com')
            ->then(fn ($response) =>  strlen($response->body()))
    );
    $pool->as('laravel', function(PendingRequest $pr) {
        return $pr->get('https://laravel.com')->then(fn ($response) =>  strlen($response->body()));
    });
});

dd($response);
/*
array:2 [
  "cosmatech" => 9095
  "laravel" => 1422023
]
*/

Alternative

I do not feel like this is necessarily the most ergonomic solution as it's kinda callback hell, but it's a pretty big retooling to make this work otherwise.

We could allow forwarding calls to the PendingRequest to the underlying promise if the request is async. 🤷 I tried making this work with a __call method on PendingRequest, but that still has the same problem where we are mutating state but it's not associated with the request. I think we would either need a Laravel-owned Promise that allows for fluent chaining (this isn't necessarily easy, since the Guzzle Promise has a lot of private state that we cannot access).

Or maybe the PendingRequest is always based on Promises?

@cosmastech cosmastech marked this pull request as draft November 28, 2025 14:41
@cosmastech cosmastech marked this pull request as ready for review November 28, 2025 14:57
@cosmastech cosmastech marked this pull request as draft November 28, 2025 16:36
@cosmastech
Copy link
Contributor Author

Closing in favor of this approach which has nicer ergonomics. #57967

@cosmastech cosmastech closed this Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant