php-standard-library/async
Fiber-based async primitives for PHP: structured concurrency with cooperative multitasking. Run tasks concurrently, manage lifecycles, cancellations, and scopes predictably. Part of PHP Standard Library; docs and guides at php-standard-library.dev.
spatie/async). It bridges the gap between synchronous Laravel defaults (e.g., Eloquent, routes) and async workflows without requiring a full ReactPHP/Swoole overhaul.Async::run() without rewriting entire pipelines. Example:
// Sync route handler → Async
Route::get('/data', function () {
return Async::run(fn() => [
fetchUserData(),
fetchOrderData(),
]);
});
// Fan-out: Dispatch parallel tasks
$tasks = collect($orders)->map(fn($order) =>
Async::task(fn() => processOrder($order))
);
Async::all($tasks)->await(); // Fan-in
Async::onEvent()) for real-time workflows (e.g., webhooks, notifications) without blocking the request lifecycle.Async::run() sparingly; prefer queues for long-running tasks.Async::task(fn() => User::find(1))) break Laravel’s connection pooling. Workaround: Use raw PDO or queue jobs for DB-bound async tasks.await() properly. Example:
public function handle(Request $request, Closure $next) {
return Async::run(fn() => $next($request))->await();
}
Illuminate\Support\Facades\Promise, but this package’s implementation may differ in:
Async::catch() integrates with Laravel’s App\Exceptions\Handler.Async::then() works with Laravel’s Promise facades.Async::fake()).Async::assertAwaited()).max_execution_time.Async::catch() → report()).spatie/async or amphp/async (duplicate Promise implementations).illuminate/support (Promise facades).App\Exceptions\Handler?spatie/async or Swoole for throughput and latency?Async::later() → dispatch()).illuminate/support (test compatibility).Async::onEvent() for real-time workflows).react/promise, ensure compatibility.Async::parallel()).spatie/async or amphp/async.Async::task() to validate gains.// Before: Queue job
ProcessPayment::dispatch($userId);
// After: Async task (still runs in worker)
Async::task(fn() => ProcessPayment::run($userId))->await();
Async::parallel() for fan-out (e.g., API aggregators):
$results = Async::parallel([
fn() => fetchUserData($userId),
fn() => fetchOrderData($orderId),
])->await();
Async::then()/Async::catch().public function handle(Request $request, Closure $next) {
return Async::run(fn() => $next($request))->await();
}
Async task durations in Sentry/New Relic.Async::catch() → report().$this->app->singleton(Async::class, fn() => new Async());
config/async.php for:
Async::setConcurrency(5)).Async in unit tests:
Async::shouldReceive('task')->andReturn(new MockPromise());
refreshDatabase() with async seeders.Async vs. queues.illuminate/support (How can I help you explore Laravel packages today?