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