@@ -28,6 +28,8 @@ function sleep($time, LoopInterface $loop)
2828 * Once the promise is resolved, this will return whatever the promise resolves to.
2929 *
3030 * Once the promise is rejected, this will throw whatever the promise rejected with.
31+ * If the promise did not reject with an `Exception`, then this function will
32+ * throw an `UnexpectedValueException` instead.
3133 *
3234 * If no $timeout is given and the promise stays pending, then this will
3335 * potentially wait/block forever until the promise is settled.
@@ -47,6 +49,7 @@ function await(PromiseInterface $promise, LoopInterface $loop, $timeout = null)
4749 $ wait = true ;
4850 $ resolved = null ;
4951 $ exception = null ;
52+ $ rejected = false ;
5053
5154 if ($ timeout !== null ) {
5255 $ promise = Timer \timeout ($ promise , $ timeout , $ loop );
@@ -58,8 +61,9 @@ function ($c) use (&$resolved, &$wait, $loop) {
5861 $ wait = false ;
5962 $ loop ->stop ();
6063 },
61- function ($ error ) use (&$ exception , &$ wait , $ loop ) {
64+ function ($ error ) use (&$ exception , &$ rejected , & $ wait , $ loop ) {
6265 $ exception = $ error ;
66+ $ rejected = true ;
6367 $ wait = false ;
6468 $ loop ->stop ();
6569 }
@@ -69,7 +73,15 @@ function ($error) use (&$exception, &$wait, $loop) {
6973 $ loop ->run ();
7074 }
7175
72- if ($ exception !== null ) {
76+ if ($ rejected ) {
77+ if (!$ exception instanceof \Exception) {
78+ $ exception = new \UnexpectedValueException (
79+ 'Promise rejected with unexpected value of type ' . (is_object (($ exception ) ? get_class ($ exception ) : gettype ($ exception ))),
80+ 0 ,
81+ $ exception instanceof \Throwable ? $ exception : null
82+ );
83+ }
84+
7385 throw $ exception ;
7486 }
7587
@@ -139,6 +151,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
139151 *
140152 * If ANY promise fails to resolve, this will try to cancel() all
141153 * remaining promises and throw an Exception.
154+ * If the promise did not reject with an `Exception`, then this function will
155+ * throw an `UnexpectedValueException` instead.
142156 *
143157 * If no $timeout is given and either promise stays pending, then this will
144158 * potentially wait/block forever until the last promise is settled.
0 commit comments