Skip to content

Commit 97a6ad3

Browse files
authored
Merge pull request #19 from WyriHaximus-labs/make-async-return-a-callable
Make `async` return a callable
2 parents 80aa19f + 4355fcf commit 97a6ad3

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/functions.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* Execute an async Fiber-based function to "await" promises.
1515
*
1616
* @param callable(mixed ...$args):mixed $function
17-
* @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is
18-
* @return PromiseInterface<mixed>
17+
* @return callable(): PromiseInterface<mixed>
1918
* @since 4.0.0
2019
* @see coroutine()
2120
*/
22-
function async(callable $function, mixed ...$args): PromiseInterface
21+
function async(callable $function): callable
2322
{
24-
return new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
23+
return static fn (mixed ...$args): PromiseInterface => new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
2524
$fiber = new \Fiber(function () use ($resolve, $reject, $function, $args): void {
2625
try {
2726
$resolve($function(...$args));

tests/AsyncTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testAsyncReturnsPendingPromise()
1515
{
1616
$promise = async(function () {
1717
return 42;
18-
});
18+
})();
1919

2020
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
2121
}
@@ -24,7 +24,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns(
2424
{
2525
$promise = async(function () {
2626
return 42;
27-
});
27+
})();
2828

2929
$value = await($promise);
3030

@@ -35,7 +35,7 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
3535
{
3636
$promise = async(function () {
3737
throw new \RuntimeException('Foo', 42);
38-
});
38+
})();
3939

4040
$this->expectException(\RuntimeException::class);
4141
$this->expectExceptionMessage('Foo');
@@ -51,7 +51,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA
5151
});
5252

5353
return await($promise);
54-
});
54+
})();
5555

5656
$value = await($promise);
5757

@@ -66,15 +66,15 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA
6666
});
6767

6868
return await($promise);
69-
});
69+
})();
7070

71-
$promise2 = async(function () {
72-
$promise = new Promise(function ($resolve) {
73-
Loop::addTimer(0.11, fn () => $resolve(42));
71+
$promise2 = async(function (int $theAnswerToLifeTheUniverseAndEverything): int {
72+
$promise = new Promise(function ($resolve) use ($theAnswerToLifeTheUniverseAndEverything): void {
73+
Loop::addTimer(0.11, fn () => $resolve($theAnswerToLifeTheUniverseAndEverything));
7474
});
7575

7676
return await($promise);
77-
});
77+
})(42);
7878

7979
$time = microtime(true);
8080
$values = await(all([$promise1, $promise2]));

tests/AwaitTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
160160
public function provideAwaiters(): iterable
161161
{
162162
yield 'await' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await($promise)];
163-
yield 'async' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await(React\Async\async(static fn(): mixed => $promise))];
163+
yield 'async' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await(React\Async\async(static fn(): mixed => $promise)())];
164164
}
165165
}

0 commit comments

Comments
 (0)