@@ -33,6 +33,8 @@ function sleep($time, LoopInterface $loop)
3333 * Once the promise is resolved, this will return whatever the promise resolves to.
3434 *
3535 * Once the promise is rejected, this will throw whatever the promise rejected with.
36+ * If the promise did not reject with an `Exception`, then this function will
37+ * throw an `UnexpectedValueException` instead.
3638 *
3739 * If no $timeout is given and the promise stays pending, then this will
3840 * potentially wait/block forever until the promise is settled.
@@ -54,6 +56,7 @@ function await(PromiseInterface $promise, LoopInterface $loop, $timeout = null)
5456 $ wait = true ;
5557 $ resolved = null ;
5658 $ exception = null ;
59+ $ rejected = false ;
5760
5861 if ($ timeout !== null ) {
5962 $ promise = Timer \timeout ($ promise , $ timeout , $ loop );
@@ -65,8 +68,9 @@ function ($c) use (&$resolved, &$wait, $loop) {
6568 $ wait = false ;
6669 $ loop ->stop ();
6770 },
68- function ($ error ) use (&$ exception , &$ wait , $ loop ) {
71+ function ($ error ) use (&$ exception , &$ rejected , & $ wait , $ loop ) {
6972 $ exception = $ error ;
73+ $ rejected = true ;
7074 $ wait = false ;
7175 $ loop ->stop ();
7276 }
@@ -76,7 +80,15 @@ function ($error) use (&$exception, &$wait, $loop) {
7680 $ loop ->run ();
7781 }
7882
79- if ($ exception !== null ) {
83+ if ($ rejected ) {
84+ if (!$ exception instanceof \Exception) {
85+ $ exception = new \UnexpectedValueException (
86+ 'Promise rejected with unexpected value of type ' . (is_object (($ exception ) ? get_class ($ exception ) : gettype ($ exception ))),
87+ 0 ,
88+ $ exception instanceof \Throwable ? $ exception : null
89+ );
90+ }
91+
8092 throw $ exception ;
8193 }
8294
@@ -148,6 +160,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
148160 *
149161 * If ANY promise fails to resolve, this will try to cancel() all
150162 * remaining promises and throw an Exception.
163+ * If the promise did not reject with an `Exception`, then this function will
164+ * throw an `UnexpectedValueException` instead.
151165 *
152166 * If no $timeout is given and either promise stays pending, then this will
153167 * potentially wait/block forever until the last promise is settled.
0 commit comments