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.
Async::parallel()).Async::onEvent()-like patterns.Promise (Laravel 8+) and queue system, enabling gradual adoption. For example:
Async::task() for short-lived async operations in controllers.dispatch()) for long-running or distributed tasks.Async to Laravel’s DI container for dependency injection.Async facade (e.g., Async::task()) to match Laravel’s conventions.Async::later() to defer tasks to Laravel’s queue system for distributed execution.Illuminate\Support\Facades\Promise, this package’s implementation may differ in:
App\Exceptions\Handler).Async::then()/Async::catch() align with team preferences (e.g., vs. spatie/async).DB::async()) can leak connections or corrupt transactions. Mitigation:
DB::connection()->getPdo() for async queries.Async::task() to wrap queue jobs (e.g., Async::task(fn() => ProcessPaymentJob::dispatch($data))).Async::run() to background jobs or async middleware.Async::timeout(5) to prevent hanging.Async::catch()).Async::fake() (if provided) or mock Async in tests:
Async::shouldReceive('task')->andReturn(new MockPromise());
refreshDatabase() and async seeders.spatie/async or amphp/async (avoid duplication).illuminate/support (Promise facades).Async for short-lived tasks, queues for long-running ones.)Illuminate\Queue).spatie/async or Swoole for high-concurrency scenarios.Async::run() in middleware without error handling).Async::later() → dispatch()).illuminate/support for hybrid sync/async code (e.g., Async::task() → Promise::all()).Async::onEvent()).Async::withSwoole()).react/promise, test compatibility (e.g., Async::fromReactPromise()).Async::parallel()).spatie/async or this package).Async::withAmphp()).Phase 1: Proof of Concept (1–2 weeks)
Async::task() to validate gains.// Before: Queue job
ProcessPaymentJob::dispatch($userId);
// After: Async task (in a controller)
Async::task(fn() => ProcessPayment::run($userId))->await();
Phase 2: Composable Workflows (2–3 weeks)
Async::parallel() for fan-out (e.g., API aggregators):
$results = Async::parallel([
fn() => fetchUserData($userId),
fn() => fetchOrderData($orderId),
])->await();
Async::then()/Async::catch():
Async::task($task)
->then(fn($result) => log($result))
->catch(fn($e) => report($e));
$this->app->singleton(Async::class, fn() => new Async());
Phase 3: Request Pipeline (3–4 weeks)
public function handle(Request $request, Closure $next) {
return Async::run(fn() => $next($request))
->then(fn(Response $response) => $response)
->catch(fn(Throwable $e) => abort(500, $e->getMessage()));
}
laravel-shift/async-testing or custom scripts.Phase 4: Observability (Ongoing)
Async task durations in Sentry/New Relic.Async::debug() middleware for development.Async::task($task)
->then(fn() => Log::debug('Task completed'))
->catch(fn($e) => Log::error('Task failed', ['error' => $e]));
react/promise (test compatibility).How can I help you explore Laravel packages today?