8000 Make `async` return a callable by WyriHaximus · Pull Request #19 · reactphp/async · GitHub
[go: up one dir, main page]

Skip to content

Make async return a callable #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
* @return callable(): PromiseInterface<mixed>
* @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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return static fn (mixed ...$args): PromiseInterface => new Promise(function (callable $resolve, callable $reject) use ($function, $args): void {
return static fn (): PromiseInterface => new Promise(function (callable $resolve, callable $reject) use ($function): void {

as discussed here: https://github.com/reactphp/async/pull/19/files#r773363277

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azjezz Only dropped the docblock item as this suggested change will break it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azjezz The changes LGTM as is, there's definitely value in being able to pass additional arguments through to the original function.

$fiber = new \Fiber(function () use ($resolve, $reject, $function, $args): void {
try {
$resolve($function(...$args));
Expand Down
18 changes: 9 additions & 9 deletions tests/AsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testAsyncReturnsPendingPromise()
{
$promise = async(function () {
return 42;
});
})();

$promise->then($this->expectCallableNever(), $this->expectCallableNever());
}
Expand All @@ -24,7 +24,7 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns(
{
$promise = async(function () {
return 42;
});
})();

$value = await($promise);

Expand All @@ -35,7 +35,7 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
{
$promise = async(function () {
throw new \RuntimeException('Foo', 42);
});
})();

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

return await($promise);
});
})();

$value = await($promise);

Expand All @@ -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]));
Expand Down
2 changes: 1 addition & 1 deletion tests/AwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)())];
}
}
0