8000
We read every piece of feedback, and take your input very seriously.
async
1 parent 80aa19f commit 8105817Copy full SHA for 8105817
src/functions.php
@@ -15,13 +15,13 @@
15
*
16
* @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>
+ * @return callable<PromiseInterface<mixed>>
19
* @since 4.0.0
20
* @see coroutine()
21
*/
22
-function async(callable $function, mixed ...$args): PromiseInterface
+function async(callable $function, mixed ...$args): callable
23
{
24
- return new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
+ return static fn (): PromiseInterface => new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
25
$fiber = new \Fiber(function () use ($resolve, $reject, $function, $args): void {
26
try {
27
$resolve($function(...$args));
tests/AsyncTest.php
@@ -15,7 +15,7 @@ public function testAsyncReturnsPendingPromise()
$promise = async(function () {
return 42;
- });
+ })();
10000 div>
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
}
@@ -24,7 +24,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns(
28
29
$value = await($promise);
30
@@ -35,7 +35,7 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
35
36
37
throw new \RuntimeException('Foo', 42);
38
39
40
$this->expectException(\RuntimeException::class);
41
$this->expectExceptionMessage('Foo');
@@ -51,7 +51,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA
51
});
52
53
return await($promise);
54
55
56
57
@@ -66,15 +66,15 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA
66
67
68
69
70
71
$promise2 = async(function () {
72
$promise = new Promise(function ($resolve) {
73
Loop::addTimer(0.11, fn () => $resolve(42));
74
75
76
77
78
79
$time = microtime(true);
80
$values = await(all([$promise1, $promise2]));
tests/AwaitTest.php
@@ -160,6 +160,6 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
160
public function provideAwaiters(): iterable
161
162
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))];
+ yield 'async' => [static fn (React\Promise\PromiseInterface $promise): mixed => React\Async\await(React\Async\async(static fn(): mixed => $promise)())];
164
165