51
51
function await (PromiseInterface $ promise )
52
52
{
53
53
$ wait = true ;
54
- $ resolved = null ;
55
- $ exception = null ;
54
+ $ resolved = false ;
56
55
$ rejected = false ;
56
+ $ resolvedValue = null ;
57
+ $ rejectedThrowable = null ;
57
58
58
59
$ promise ->then (
59
- function ($ c ) use (&$ resolved , &$ wait ) {
60
- $ resolved = $ c ;
60
+ function ($ c ) use (&$ resolved , &$ resolvedValue , &$ wait ) {
61
+ $ resolvedValue = $ c ;
62
+ $ resolved = true ;
61
63
$ wait = false ;
62
64
Loop::stop ();
63
65
},
64
- function ($ error ) use (&$ exception , &$ rejected , &$ wait ) {
65
- $ exception = $ error ;
66
+ function ($ error ) use (&$ rejected , &$ rejectedThrowable , &$ wait ) {
67
+ $ rejectedThrowable = $ error ;
66
68
$ rejected = true ;
67
69
$ wait = false ;
68
70
Loop::stop ();
@@ -73,22 +75,37 @@ function ($error) use (&$exception, &$rejected, &$wait) {
73
75
// argument does not show up in the stack trace in PHP 7+ only.
74
76
$ promise = null ;
75
77
78
+ if ($ rejected ) {
79
+ // promise is rejected with an unexpected value (Promise API v1 or v2 only)
80
+ if (!$ rejectedThrowable instanceof \Exception && !$ rejectedThrowable instanceof \Throwable) {
81
+ $ rejectedThrowable = new \UnexpectedValueException (
82
+ 'Promise rejected with unexpected value of type ' . (is_object ($ rejectedThrowable ) ? get_class ($ rejectedThrowable ) : gettype ($ rejectedThrowable ))
83
+ );
84
+ }
85
+
86
+ throw $ rejectedThrowable ;
87
+ }
88
+
89
+ if ($ resolved ) {
90
+ return $ resolvedValue ;
91
+ }
92
+
76
93
while ($ wait ) {
77
94
Loop::run ();
78
95
}
79
96
80
97
if ($ rejected ) {
81
98
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
82
- if (!$ exception instanceof \Exception && !$ exception instanceof \Throwable) {
83
- $ exception = new \UnexpectedValueException (
84
- 'Promise rejected with unexpected value of type ' . (is_object ($ exception ) ? get_class ($ exception ) : gettype ($ exception ))
99
+ if (!$ rejectedThrowable instanceof \Exception && !$ rejectedThrowable instanceof \Throwable) {
100
+ $ rejectedThrowable = new \UnexpectedValueException (
101
+ 'Promise rejected with unexpected value of type ' . (is_object ($ rejectedThrowable ) ? get_class ($ rejectedThrowable ) : gettype ($ rejectedThrowable ))
85
102
);
86
103
}
87
104
88
- throw $ exception ;
105
+ throw $ rejectedThrowable ;
89
106
}
90
107
91
- return $ resolved ;
108
+ return $ resolvedValue ;
92
109
}
93
110
94
111
/**
0 commit comments