Skip to content

Use react/async for promise wait #50

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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
tools: composer
coverage: xdebug

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
}
],
"require": {
"php": "^7.1|^8.0",
"php": "^8.1",
"php-http/httplug": "^2.0",
"react/http": "^1.0",
"react/event-loop": "^1.2",
"php-http/discovery": "^1.0",
"phpunit/phpunit": "^9.3.11 || ^7"
"react/async": "^4"
},
"require-dev": {
"php-http/client-integration-tests": "^3.0",
"php-http/message": "^1.0",
"phpunit/phpunit": "^9.5",
"nyholm/psr7": "^1.3"
},
"provide": {
Expand Down
12 changes: 6 additions & 6 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Http\Promise\Promise as HttpPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

use function React\Async\await;

use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use RuntimeException;
Expand Down Expand Up @@ -114,12 +117,9 @@ public function getState()
*/
public function wait($unwrap = true)
{
$loop = $this->loop;
while (HttpPromise::PENDING === $this->getState()) {
$loop->futureTick(function () use ($loop) {
$loop->stop();
});
$loop->run();
try {
await($this->promise);
} catch (\Throwable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it correct to eat away the exception and not do anything with it? seems dangerous to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Check lines 82 to 96, and 123 to 129. In theory that will cover the swallowing here. This is something I'm considering changing but not sure what the best approach is yet

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, i see yep. thanks for the explanation.

what happens with line 128 if there was an exception and we chose not to unwrap? the response will be null, right? but that will have been the same before if i understand correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I kept the behavior the same as before. Not unwrapping is a very conscious call to swallow any error that might come up.

}

if ($unwrap) {
Expand Down