@@ -24,11 +24,16 @@ public function __construct(LoopInterface $loop)
2424 */
2525 public function wait ($ time )
2626 {
27+ $ wait = true ;
2728 $ loop = $ this ->loop ;
28- $ loop ->addTimer ($ time , function () use ($ loop ) {
29+ $ loop ->addTimer ($ time , function () use ($ loop, & $ wait ) {
2930 $ loop ->stop ();
31+ $ wait = false ;
3032 });
31- $ loop ->run ();
33+
34+ do {
35+ $ loop ->run ();
36+ } while ($ wait );
3237 }
3338
3439 /**
@@ -40,20 +45,26 @@ public function wait($time)
4045 */
4146 public function awaitOne (PromiseInterface $ promise )
4247 {
48+ $ wait = true ;
4349 $ resolved = null ;
4450 $ exception = null ;
51+ $ loop = $ this ->loop ;
4552
4653 $ promise ->then (
47- function ($ c ) use (&$ resolved ) {
54+ function ($ c ) use (&$ resolved, & $ wait , $ loop ) {
4855 $ resolved = $ c ;
56+ $ wait = false ;
57+ $ loop ->stop ();
4958 },
50- function ($ error ) use (&$ exception ) {
59+ function ($ error ) use (&$ exception, & $ wait , $ loop ) {
5160 $ exception = $ error ;
61+ $ wait = false ;
62+ $ loop ->stop ();
5263 }
5364 );
5465
55- while ($ resolved === null && $ exception === null ) {
56- $ this -> loop ->tick ();
66+ while ($ wait ) {
67+ $ loop ->run ();
5768 }
5869
5970 if ($ exception !== null ) {
@@ -80,11 +91,12 @@ public function awaitRace(array $promises)
8091 $ wait = count ($ promises );
8192 $ value = null ;
8293 $ success = false ;
94+ $ loop = $ this ->loop ;
8395
8496 foreach ($ promises as $ key => $ promise ) {
8597 /* @var $promise PromiseInterface */
8698 $ promise ->then (
87- function ($ return ) use (&$ value , &$ wait , &$ success , $ promises ) {
99+ function ($ return ) use (&$ value , &$ wait , &$ success , $ promises, $ loop ) {
88100 if (!$ wait ) {
89101 // only store first promise value
90102 return ;
@@ -99,19 +111,25 @@ function ($return) use (&$value, &$wait, &$success, $promises) {
99111 $ promise ->cancel ();
100112 }
101113 }
114+
115+ $ loop ->stop ();
102116 },
103- function ($ e ) use (&$ wait ) {
117+ function ($ e ) use (&$ wait, $ loop ) {
104118 if ($ wait ) {
105119 // count number of promises to await
106120 // cancelling promises will reject all remaining ones, ignore this
107121 --$ wait ;
122+
123+ if (!$ wait ) {
124+ $ loop ->stop ();
125+ }
108126 }
109127 }
110128 );
111129 }
112130
113131 while ($ wait ) {
114- $ this -> loop ->tick ();
132+ $ loop ->run ();
115133 }
116134
117135 if (!$ success ) {
@@ -140,15 +158,20 @@ public function awaitAll(array $promises)
140158 $ wait = count ($ promises );
141159 $ exception = null ;
142160 $ values = array ();
161+ $ loop = $ this ->loop ;
143162
144163 foreach ($ promises as $ key => $ promise ) {
145164 /* @var $promise PromiseInterface */
146165 $ promise ->then (
147- function ($ value ) use (&$ values , $ key , &$ wait ) {
166+ function ($ value ) use (&$ values , $ key , &$ wait, $ loop ) {
148167 $ values [$ key ] = $ value ;
149168 --$ wait ;
169+
170+ if (!$ wait ) {
171+ $ loop ->stop ();
172+ }
150173 },
151- function ($ e ) use ($ promises , &$ exception , &$ wait ) {
174+ function ($ e ) use ($ promises , &$ exception , &$ wait, $ loop ) {
152175 if (!$ wait ) {
153176 // cancelling promises will reject all remaining ones, only store first error
154177 return ;
@@ -163,12 +186,14 @@ function ($e) use ($promises, &$exception, &$wait) {
163186 $ promise ->cancel ();
164187 }
165188 }
189+
190+ $ loop ->stop ();
166191 }
167192 );
168193 }
169194
170195 while ($ wait ) {
171- $ this -> loop ->tick ();
196+ $ loop ->run ();
172197 }
173198
174199 if ($ exception !== null ) {
0 commit comments