php-http/promise
Common Promise implementation for PHP-HTTP clients. Provides a lightweight, interoperable way to handle async HTTP responses with then/catch chaining, wait/cancel support, and consistent behavior across multiple HTTP client adapters and PSR-7 message workflows.
Start by understanding that this package provides a lightweight PSR-18-compatible Promise interface for handling asynchronous HTTP operations—commonly used as a bridge between HTTPlug and PSR-18/PSR-7 ecosystems. If you’re building or consuming async HTTP clients (e.g., with Guzzle 7+, Symfony HttpClient, or custom adapters), this promise enables you to write non-blocking code without bloating dependencies.
First use case: you have a PSR-18 client and want to make multiple requests concurrently. You wrap each client call in a Http\Promise\Promise (or Http\Promise\FulfilledPromise/RejectedPromise) and use Promise::when() or async() methods to resolve them collectively.
Check src/Promise.php, src/FulfilledPromise.php, and src/RejectedPromise.php for core classes. Readme docs are minimal—look at usage in dependencies like php-http/discovery or php-http/guzzle6-adapter for real-world integration.
$promise = new Promise(function () use ($client, $request) {
$response = $client->sendRequest($request);
return $response->getStatusCode();
});
$promise->then(function ($status) { /* handle */ });
Promise::when() to resolve when all complete:
$promises = array_map(function ($url) use ($client, $requestFactory) {
$request = $requestFactory->createRequest('GET', $url);
return new Promise(function () use ($client, $request) {
return $client->sendRequest($request);
});
}, $urls);
$results = Promise\when($promises)->wait();
Promise with FulfilledPromise/RejectedPromise to simulate success/failure paths without real network calls.HandlerStack with multi-handle). Don’t expect ReactPHP-like behavior unless the client supports it.wait() blocks synchronously: Calling $promise->wait() halts execution until resolved. Use it only in CLI scripts or explicit async integrations.\Exception objects—always handle rejections via then(null, $onRejected) or await() with try/catch.Http facade doesn’t use it), but important when developing Laravel packages that need PSR-18/HTTPlug async compatibility.Throwable in signatures—ensure your then() callbacks expect both \Exception and \Throwable.How can I help you explore Laravel packages today?