internal/promise
Lightweight Promises/A implementation for PHP (fork of reactphp/promise). PHP 8.1+ compatible with strict types and improved type annotations. Drop-in replacement for react/promise v2/v3 with reusable rejection handling and safer defaults.
spatie/async).Promise.all() for parallel API calls) without callback hell, reducing cognitive complexity.queue:work callbacks with Deferred promises for chained async tasks.Promise.race() to implement timeouts for external APIs.finally() for cleanup (e.g., logging, resource release).resolve(T $value)) catch bugs early, improving maintainability.reactphp/promise with 1:1 API parity (same methods: then(), catch(), all(), etc.).Illuminate\Support\Facades via custom facades (e.g., Promise::resolve()).| Risk Area | Mitigation Strategy |
|---|---|
| BC Breaks | v3.x is stable; v2.x still supported for legacy. Test migration path (see below). |
| Error Handling | Unhandled rejections logged by default (configurable via set_rejection_handler()). |
| Performance | Benchmark against reactphp/promise; minimal overhead for I/O-bound tasks. |
| Fiber Support | Avoids iterative handlers (better Fiber compatibility), but lacks native Fiber APIs. |
| Thread Safety | Promises are single-threaded by design; safe for Laravel’s request lifecycle. |
set_rejection_handler() to integrate with Laravel’s logging (e.g., Log::error())?all() vs. race()).Guzzle\Promise calls in internal/promise for unified error handling.Deferred to chain jobs (e.g., dispatch()->then(fn() => dispatchNext())).socket.on('message')->then(...)).DB::select() callbacks with Promise.resolve(DB::select(...)).User::where(...)->get()->then(...)).| Phase | Action | Tools/Examples |
|---|---|---|
| Evaluation | Benchmark against reactphp/promise and Laravel’s native async tools (e.g., spatie/async). |
haute-couture/benchmark for performance; compare callback vs. Promise code. |
| Pilot | Replace Guzzle callbacks with Promises in API services. | Example: GuzzlePromise::promise()->then(...) → Promise::resolve(Guzzle::request(...)). |
| Core Integration | Add Promise facade to Laravel (config/app.php). |
Promise::all([$job1, $job2]) for parallel queue jobs. |
| Queue Migration | Replace queue:work callbacks with Deferred chains. |
Deferred::promise()->then(fn() => dispatch('ProcessPayment')). |
| Error Handling | Extend set_rejection_handler() to log to Laravel’s Log channel. |
Promise::set_rejection_handler(fn(Throwable $e) => Log::error($e));. |
otherwise()), but provides BC aliases (e.g., catch() replaces otherwise()).Promise as a singleton in AppServiceProvider.Promise facade for resolve(), reject(), etc.Mockery to stub Promises in unit tests.internal/promise to composer.json.Deferred.Promise facade and global rejection handler.Promise.all() for batch processing).internal/promise is standalone).then/catch/finally).expectPending().set_rejection_handler()).Promise::race([$job1, $job2]) for timeout logic.Promise.all() scales horizontally (e.g., batch API calls).cancel() for long-running Promises (e.g., user-initiated timeouts).| Failure Scenario | Mitigation |
|---|---|
| Unhandled Rejections | Global handler logs to Laravel’s Log channel. |
| Memory Leaks | Promises are garbage-collected; avoid circular references. |
| Timeouts | Use Promise.race() with a timeout Promise (e.g., sleep(5)->then(...)). |
| Cancellation Issues | Ensure cancel() is called in finally() blocks for cleanup. |
| PHP Version Incompatibility | Pin to ^3.0 for PHP 8.1+; use v2.x for legacy. |
then/catch, all/race).How can I help you explore Laravel packages today?