From e629cc81e7b3211f13da5b5d09751e0636f1222c Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 4 Dec 2021 10:01:56 -0600 Subject: [PATCH] Add optional args to async (#379) --- src/functions.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/functions.php b/src/functions.php index ce0e3a1..d935215 100644 --- a/src/functions.php +++ b/src/functions.php @@ -13,16 +13,17 @@ use Revolt\EventLoop\UnsupportedFeatureException; * @template T * * @param \Closure():T $closure + * @param mixed ...$args Arguments forwarded to the closure when starting the fiber. * * @return Future */ -function async(\Closure $closure): Future +function async(\Closure $closure, mixed ...$args): Future { static $run = null; - $run ??= static function (FutureState $state, \Closure $closure): void { + $run ??= static function (FutureState $state, \Closure $closure, array $args): void { try { - $state->complete($closure()); + $state->complete($closure(...$args)); } catch (\Throwable $exception) { $state->error($exception); } @@ -30,7 +31,7 @@ function async(\Closure $closure): Future $state = new Internal\FutureState; - EventLoop::queue($run, $state, $closure); + EventLoop::queue($run, $state, $closure, $args); return new Future($state); }