|
27 | 27 | * resolved to. |
28 | 28 | * |
29 | 29 | * Once the promise is rejected, this will throw whatever the promise rejected |
30 | | - * with. If the promise did not reject with an `Exception`, then this function |
31 | | - * will throw an `UnexpectedValueException` instead. |
| 30 | + * with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+), |
| 31 | + * then this function will throw an `UnexpectedValueException` instead. |
32 | 32 | * |
33 | 33 | * ```php |
34 | 34 | * try { |
35 | 35 | * $result = React\Async\await($promise, $loop); |
36 | 36 | * // promise successfully fulfilled with $result |
37 | 37 | * echo 'Result: ' . $result; |
38 | | - * } catch (Exception $exception) { |
39 | | - * // promise rejected with $exception |
40 | | - * echo 'ERROR: ' . $exception->getMessage(); |
| 38 | + * } catch (Throwable $e) { |
| 39 | + * // promise rejected with $e |
| 40 | + * echo 'Error: ' . $e->getMessage(); |
41 | 41 | * } |
42 | 42 | * ``` |
43 | 43 | * |
44 | 44 | * @param PromiseInterface $promise |
45 | 45 | * @return mixed returns whatever the promise resolves to |
46 | | - * @throws \Exception when the promise is rejected |
| 46 | + * @throws \Exception when the promise is rejected with an `Exception` |
| 47 | + * @throws \Throwable when the promise is rejected with a `Throwable` (PHP 7+) |
| 48 | + * @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only) |
47 | 49 | */ |
48 | 50 | function await(PromiseInterface $promise) |
49 | 51 | { |
@@ -75,16 +77,11 @@ function ($error) use (&$exception, &$rejected, &$wait) { |
75 | 77 | } |
76 | 78 |
|
77 | 79 | if ($rejected) { |
| 80 | + // promise is rejected with an unexpected value (Promise API v1 or v2 only) |
78 | 81 | if (!$exception instanceof \Exception && !$exception instanceof \Throwable) { |
79 | 82 | $exception = new \UnexpectedValueException( |
80 | 83 | 'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)) |
81 | 84 | ); |
82 | | - } elseif (!$exception instanceof \Exception) { |
83 | | - $exception = new \UnexpectedValueException( |
84 | | - 'Promise rejected with unexpected ' . get_class($exception) . ': ' . $exception->getMessage(), |
85 | | - $exception->getCode(), |
86 | | - $exception |
87 | | - ); |
88 | 85 | } |
89 | 86 |
|
90 | 87 | throw $exception; |
|
0 commit comments