- How do I install php-standard-library/promise in a Laravel project?
- Run `composer require php-standard-library/promise` in your project root. The package has no Laravel-specific dependencies, so it integrates seamlessly with existing codebases. No service provider or facade setup is required unless you want to extend Laravel’s native features with Promise macros.
- Can I use this package to chain HTTP requests (e.g., Guzzle) in Laravel?
- Yes. Wrap Guzzle’s async requests in Promises using `new Promise(fn() => $client->request(...))` and chain them with `.then()` for sequential execution. For parallel requests, use `Promise::all()` to aggregate results. This avoids nested callbacks and keeps your HTTP logic clean and composable.
- Does this package work with Laravel Queues or Horizon?
- It integrates indirectly. You can wrap `dispatch()` calls in Promises to chain queue jobs (e.g., `Promise::all([dispatch('Job1'), dispatch('Job2')])`), but the package itself doesn’t replace Laravel’s queue system. For monitoring, you’d need custom Telescope channels or logging to track Promise resolutions alongside Horizon.
- What PHP and Laravel versions does php-standard-library/promise support?
- The package targets PHP 8.1+ and is compatible with Laravel 9/10. It avoids heavy dependencies, so it won’t conflict with Laravel’s core. Always check the package’s `composer.json` for the latest version constraints, as minor updates may align with newer Laravel releases.
- How do I handle errors in a Promise chain in Laravel?
- Use `.catch()` to handle rejections. For global error tracking, integrate with Laravel’s logging (e.g., `Log::error($reason)` in the catch block) or Sentry. Unhandled rejections will throw exceptions, so ensure critical Promises have catch handlers. Avoid silent failures by validating inputs before resolving.
- Is this package suitable for production Laravel applications?
- Yes, but with caveats. It’s lightweight and production-ready for bounded contexts like API clients or background jobs. Avoid using it for core business logic where synchronous flows are preferred. Test Promise-heavy paths thoroughly, especially error scenarios, and monitor performance in high-throughput environments.
- Can I use Promises to replace Laravel’s Bus or Queue system entirely?
- No. This package is for *composing* async operations, not replacing Laravel’s Bus or Queue. Use Promises to chain jobs or HTTP calls *within* a queue worker or command, but rely on Laravel’s Queues for actual async execution. For example, dispatch a job, then chain Promises to process its result.
- How do I test Promise-based code in Laravel?
- Mock Promises with Mockery by stubbing their `resolve()` or `reject()` methods. For example, `Mockery::mock('alias:Promise')->shouldReceive('then')->andReturnSelf()->once()`. Test both success and failure paths. Use Laravel’s `expectException()` to verify rejections. Avoid testing implementation details; focus on behavior.
- Are there performance concerns with Promises in Laravel?
- Minimal overhead for most use cases, but batch operations (e.g., `Promise::all()` with 100+ Promises) may impact memory. For high-load scenarios, consider parallelism limits or lazy resolution. Profile with Laravel Forge or Blackfire to identify bottlenecks, especially when mixing Promises with synchronous Laravel code.
- What alternatives exist for async workflows in Laravel?
- For simple async tasks, Laravel’s built-in `Bus` or `Queue` may suffice. For more complex composition, consider ReactPHP (event-loop based) or Amp (fiber-based), but they’re heavier. This package strikes a balance: lightweight, callback-free, and Laravel-friendly. Use it when you need chaining but don’t want a full async framework.