1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00

Add optional args to async (#379)

This commit is contained in:
Aaron Piotrowski 2021-12-04 10:01:56 -06:00 committed by GitHub
parent b8764391dd
commit e629cc81e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,16 +13,17 @@ use Revolt\EventLoop\UnsupportedFeatureException;
* @template T * @template T
* *
* @param \Closure():T $closure * @param \Closure():T $closure
* @param mixed ...$args Arguments forwarded to the closure when starting the fiber.
* *
* @return Future<T> * @return Future<T>
*/ */
function async(\Closure $closure): Future function async(\Closure $closure, mixed ...$args): Future
{ {
static $run = null; static $run = null;
$run ??= static function (FutureState $state, \Closure $closure): void { $run ??= static function (FutureState $state, \Closure $closure, array $args): void {
try { try {
$state->complete($closure()); $state->complete($closure(...$args));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$state->error($exception); $state->error($exception);
} }
@ -30,7 +31,7 @@ function async(\Closure $closure): Future
$state = new Internal\FutureState; $state = new Internal\FutureState;
EventLoop::queue($run, $state, $closure); EventLoop::queue($run, $state, $closure, $args);
return new Future($state); return new Future($state);
} }