8000 Add new `await()` function (import from clue/reactphp-block) by clue · Pull Request #8 · reactphp/async · GitHub
[go: up one dir, main page]

Skip to content

Add new await() function (import from clue/reactphp-block) #8

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 5 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Support throwing Throwable as-is (PHP 7+)
  • Loading branch information
clue committed Oct 21, 2021
commit d2a660855053aa8a779a96bcb007c99cee8f202e
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ Once the promise is fulfilled, this function will return whatever the promise
resolved to.

Once the promise is rejected, this will throw whatever the promise rejected
with. If the promise did not reject with an `Exception`, then this function
will throw an `UnexpectedValueException` instead.
with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
then this function will throw an `UnexpectedValueException` instead.

```php
try {
$result = React\Async\await($promise);
// promise successfully fulfilled with $result
echo 'Result: ' . $result;
} catch (Exception $exception) {
// promise rejected with $exception
echo 'ERROR: ' . $exception->getMessage();
} catch (Throwable $e) {
// promise rejected with $e
echo 'Error: ' . $e->getMessage();
}
```

Expand Down
21 changes: 9 additions & 12 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@
* resolved to.
*
* Once the promise is rejected, this will throw whatever the promise rejected
* with. If the promise did not reject with an `Exception`, then this function
* will throw an `UnexpectedValueException` instead.
* with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
* then this function will throw an `UnexpectedValueException` instead.
*
* ```php
* try {
* $result = React\Async\await($promise, $loop);
* // promise successfully fulfilled with $result
* echo 'Result: ' . $result;
* } catch (Exception $exception) {
* // promise rejected with $exception
* echo 'ERROR: ' . $exception->getMessage();
* } catch (Throwable $e) {
* // promise rejected with $e
* echo 'Error: ' . $e->getMessage();
* }
* ```
*
* @param PromiseInterface $promise
* @return mixed returns whatever the promise resolves to
* @throws \Exception when the promise is rejected
* @throws \Exception when the promise is rejected with an `Exception`
* @throws \Throwable when the promise is rejected with a `Throwable` (PHP 7+)
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
*/
function await(PromiseInterface $promise)
{
Expand Down Expand Up @@ -75,16 +77,11 @@ function ($error) use (&$exception, &$rejected, &$wait) {
}

if ($rejected) {
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
);
} elseif (!$exception instanceof \Exception) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected ' . get_class($exception) . ': ' . $exception->getMessage(),
$exception->getCode(),
$exception
);
}

throw $exception;
Expand Down
14 changes: 3 additions & 11 deletions tests/AwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,12 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
/**
* @requires PHP 7
*/
public function testAwaitOneRejectedWithPhp7ErrorWillWrapInUnexpectedValueExceptionWithPrevious()
public function testAwaitRejectedWithPhp7ErrorWillThrowOriginalError()
{
$promise = Promise\reject(new \Error('Test', 42));

try {
React\Async\await($promise);
$this->fail();
} catch (\UnexpectedValueException $e) {
$this->assertEquals('Promise rejected with unexpected Error: Test', $e->getMessage());
$this->assertEquals(42, $e->getCode());
$this->assertInstanceOf('Throwable', $e->getPrevious());
$this->assertEquals('Test', $e->getPrevious()->getMessage());
$this->assertEquals(42, $e->getPrevious()->getCode());
}
$this->setExpectedException('Error', 'Test', 42);
React\Async\await($promise);
}

public function testAwaitOneResolved()
Expand Down
0