8000 Make `async` return a callable · reactphp/async@37420b0 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 37420b0

Browse files
committed
Make async return a callable
By making this change `async()` can now also by used in event loop callbacks such as timers: ```php Loop::addTimer(0.01, async(function () { echo 'Sleeping for one second'; await(asleep(1)); echo 'Waking up again'; })); ``` With this change, current `async()` usage changes from: ```php async(function () { // }); ``` To: ```php async(function () { // })(); ```
1 parent 80aa19f commit 37420b0

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*
1616
* @param callable(mixed ...$args):mixed $function
1717
* @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is
18-
* @return PromiseInterface<mixed>
18+
* @return callable(): PromiseInterface<mixed>
1919
* @since 4.0.0
2020
* @see coroutine()
2121
*/
22-
function async(callable $function, mixed ...$args): PromiseInterface
22+
function async(callable $function): callable
2323
{
24-
return new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
24+
return static fn (mixed ...$args): PromiseInterface => new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
2525
$fiber = new \Fiber(function () use ($resolve, $reject, $function, $args): void {
2626
try {
2727
$resolve($function(...$args));

tests/AsyncTest.php

Lines changed: 9 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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)
0