diff --git a/src/functions.php b/src/functions.php index c25fa05..f003db1 100644 --- a/src/functions.php +++ b/src/functions.php @@ -14,14 +14,13 @@ * Execute an async Fiber-based function to "await" promises. * * @param callable(mixed ...$args):mixed $function - * @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is - * @return PromiseInterface + * @return callable(): PromiseInterface * @since 4.0.0 * @see coroutine() */ -function async(callable $function, mixed ...$args): PromiseInterface +function async(callable $function): callable { - return new Promise(function (callable $resolve, callable $reject) use ($function, $args): void { + return static fn (mixed ...$args): PromiseInterface => new Promise(function (callable $resolve, callable $reject) use ($function, $args): void { $fiber = new \Fiber(function () use ($resolve, $reject, $function, $args): void { try { $resolve($function(...$args)); diff --git a/tests/AsyncTest.php b/tests/AsyncTest.php index de75a27..ad856cb 100644 --- a/tests/AsyncTest.php +++ b/tests/AsyncTest.php @@ -15,7 +15,7 @@ public function testAsyncReturnsPendingPromise() { $promise = async(function () { return 42; - }); + })(); $promise->then($this->expectCallableNever(), $this->expectCallableNever()); } @@ -24,7 +24,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns( { $promise = async(function () { return 42; - }); + })(); $value = await($promise); @@ -35,7 +35,7 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow { $promise = async(function () { throw new \RuntimeException('Foo', 42); - }); + })(); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Foo'); @@ -51,7 +51,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA }); return await($promise); - }); + })(); $value = await($promise); @@ -66,15 +66,15 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA }); return await($promise); - }); + })(); - $promise2 = async(function () { - $promise = new Promise(function ($resolve) { - Loop::addTimer(0.11, fn () => $resolve(42)); + $promise2 = async(function (int $theAnswerToLifeTheUniverseAndEverything): int { + $promise = new Promise(function ($resolve) use ($theAnswerToLifeTheUniverseAndEverything): void { + Loop::addTimer(0.11, fn () => $resolve($theAnswerToLifeTheUniverseAndEverything)); }); return await($promise); - }); + })(42); $time = microtime(true); $values = await(all([$promise1, $promise2])); diff --git a/tests/AwaitTest.php b/tests/AwaitTest.php index 0be7a11..782f6da 100644 --- a/tests/AwaitTest.php +++ b/tests/AwaitTest.php @@ -160,6 +160,6 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi public function provideAwaiters(): iterable { yield 'await' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await($promise)]; - yield 'async' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await(React\Async\async(static fn(): mixed => $promise))]; + yield 'async' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await(React\Async\async(static fn(): mixed => $promise)())]; } }