guzzlehttp/promises
Iterative promise chaining for infinite async operations with Guzzle HTTP. Supports Promises/A+ compliance, coroutine-style await via Coroutine::of(), and synchronous wait() method. Cancel promises mid-execution and handle rejection/fulfillment callbacks cleanly. Works with any object with a then()...
Start by installing the package: composer require guzzlehttp/promises. The core concept is simple: Promise objects represent asynchronous operations. Begin by creating a promise, attaching callbacks via then(), and resolving it manually (e.g., $promise->resolve('value')). For immediate values or rejections, use FulfilledPromise and RejectedPromise instead of manually creating promises. The most common first use case is to wrap synchronous logic in a promise for testing or to prepare for async extensions—like mocking API responses that could be async later.
$waitFn to trigger resolution. This enables uniform handling of sync and async flows.then() extensively to build pipelines—return new promises or values to chain operations. Rely on rejection forwarding (return new RejectedPromise(...)) to propagate errors cleanly.Coroutine::of() for async/await-style code—this leverages yield inside a generator to write asynchronous logic that reads synchronously.guzzle/promises is standalone, it’s tightly integrated with guzzlehttp/guzzle. In custom HTTP clients or async workers, use Utils::queue()->run() periodically in event loops (e.g., ReactPHP) to resolve pending promises.FulfilledPromise/RejectedPromise to force deterministic behavior and avoid timing issues.then callbacks must not block indefinitely—otherwise, wait() hangs. Always set timeouts or integrate with event loops via Utils::queue().wait(true) throws for rejections or unwraps nested promises. Use wait(false) if you only need to ensure resolution (e.g., cleanup), and check state manually via getState().$cancelFn during construction. If you wrap third-party code (e.g., curl multi), ensure cancellation propagates to the underlying operation.then() works with foreign promises (e.g., ReactPHP), wait()/cancel() won’t propagate across implementations. Wrap external promises with new Promise(fn) if you need synchronous control or cancellation.Utils::queue()->run() on each loop tick—otherwise, promises never resolve, even if their data is ready. For Swoole or ReactPHP, this is non-negotiable.Exception values (e.g., strings, arrays) causes wait() to throw RejectionException. Always throw real Exceptions for cleaner error handling in production.How can I help you explore Laravel packages today?