- How do I use this Promise library with Laravel’s queue system to chain dependent jobs?
- Wrap Laravel’s `dispatch()` calls in Promises and chain them using `.then()`. For example, dispatch a job, then resolve its result before dispatching the next job. Use `Promise::all()` for parallel execution or `Promise::then()` for sequential workflows. This avoids nested callbacks and keeps your queue logic clean and composable.
- Does this package support Laravel 10+ and PHP 8.1+? What about older versions?
- The package targets modern PHP (8.1+) and aligns with Laravel’s LTS support. While it may work on older versions, it’s optimized for PHP 8.1+ features like named arguments and attributes. Check the [PHP Standard Library docs](https://php-standard-library.dev) for version-specific guidance, as Laravel 9 or older may require polyfills or adjustments.
- Can I use this for HTTP requests with Guzzle or PestHTTP in Laravel?
- Yes. Convert Guzzle or PestHTTP async requests into Promises by wrapping their callbacks in the Promise constructor. Chain responses with `.then()` or handle errors with `.catch()`. For example, `Promise::resolve(guzzleRequest())->then(processResponse)`. This works well for sequential or parallel API calls in Laravel controllers or services.
- How do I handle unhandled Promise rejections in Laravel? Will they crash my app?
- Unhandled rejections won’t crash your app by default, but you should explicitly catch them using `.catch()` or a global handler. For Laravel, integrate with Sentry or Monolog by attaching a rejection listener via `Promise::onRejected()`. This ensures errors are logged or reported without silent failures, similar to JavaScript’s `unhandledrejection` event.
- Is this package suitable for high-throughput applications, or will it introduce performance bottlenecks?
- The package is lightweight and avoids heavy dependencies, but async operations still require careful handling. For high-throughput scenarios (e.g., batch processing), consider using `Promise::all()` with a limit to avoid overwhelming memory or I/O. Test under load, as PHP’s synchronous nature means Promises are resolved via callbacks, which may introduce minor overhead compared to native async frameworks like Swoole.
- How can I test Promises in Laravel’s PHPUnit or Pest tests? Are there mocking strategies?
- Use Mockery or PHPUnit’s mock builders to stub Promise resolutions. For example, mock a Promise to return a resolved value immediately during tests. Libraries like `php-standard-library/testing` may offer helpers for Promise assertions. Avoid testing async logic directly; instead, verify the final state or side effects (e.g., queued jobs, database changes) after Promise resolution.
- What’s the difference between this and Laravel’s built-in `Bus` or `Queue` system? Should I use this instead?
- Laravel’s `Bus` and `Queue` are for job dispatching and synchronous command handling, while this Promise library enables *async composition*—chaining operations like `.then()` or `.all()`. Use this for orchestrating async workflows (e.g., API calls, dependent jobs) where you need to wait for multiple operations before proceeding. For simple job queues, stick with Laravel’s native systems, but combine them with Promises for advanced async logic.
- Can I integrate this with Laravel Horizon or Telescope for monitoring?
- Not natively, but you can extend it. For Horizon, log Promise states (resolved/rejected) via Laravel’s logging channels. For Telescope, create a custom channel to track Promise execution times or failures. This requires writing a listener or facade to bridge Promises with Laravel’s observability tools, similar to how you’d instrument queue jobs.
- How do I resolve Promises in a Laravel command or event listener? Do I need an event loop?
- You don’t need an event loop—Promises are resolved via callbacks or synchronous operations. In a command, resolve Promises by passing callbacks to their constructors (e.g., `Promise::resolve(fn() => $result)`). For async operations (e.g., HTTP requests), use the library’s `then()` to chain results. Laravel’s synchronous execution model works fine; Promises just defer resolution until the callback runs.
- Are there alternatives to this package for Laravel that offer similar functionality?
- Yes. For async workflows, consider `ampphp/amp` (full event loop) or `reactphp/promise` (ReactPHP-compatible). For simpler use cases, Laravel’s `Bus` or `Queue` may suffice with manual chaining. If you need a lightweight, Laravel-friendly solution without event loops, this package is a strong choice, especially for API clients or queue orchestration. Evaluate based on your need for composability vs. performance.