@@ -20,7 +20,7 @@ public function __construct(callable $resolver, callable $canceller = null)
2020 public function then (callable $ onFulfilled = null , callable $ onRejected = null )
2121 {
2222 if (null !== $ this ->result ) {
23- return $ this ->result () ->then ($ onFulfilled , $ onRejected );
23+ return $ this ->result ->then ($ onFulfilled , $ onRejected );
2424 }
2525
2626 if (null === $ this ->canceller ) {
@@ -41,7 +41,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
4141 public function done (callable $ onFulfilled = null , callable $ onRejected = null )
4242 {
4343 if (null !== $ this ->result ) {
44- return $ this ->result () ->done ($ onFulfilled , $ onRejected );
44+ return $ this ->result ->done ($ onFulfilled , $ onRejected );
4545 }
4646
4747 $ this ->handlers [] = function (PromiseInterface $ promise ) use ($ onFulfilled , $ onRejected ) {
@@ -117,15 +117,7 @@ private function reject($reason)
117117
118118 private function settle (PromiseInterface $ result )
119119 {
120- if ($ result instanceof LazyPromise) {
121- $ result = $ result ->promise ();
122- }
123-
124- if ($ result === $ this ) {
125- $ result = new RejectedPromise (
126- new \LogicException ('Cannot resolve a promise with itself. ' )
127- );
128- }
120+ $ result = $ this ->unwrap ($ result );
129121
130122 $ handlers = $ this ->handlers ;
131123
@@ -138,13 +130,30 @@ private function settle(PromiseInterface $result)
138130 }
139131 }
140132
141- private function result ( )
133+ private function unwrap ( $ promise )
142134 {
143- while ($ this ->result instanceof self && null !== $ this ->result ->result ) {
144- $ this ->result = $ this ->result ->result ;
135+ $ promise = $ this ->extract ($ promise );
136+
137+ while ($ promise instanceof self && null !== $ promise ->result ) {
138+ $ promise = $ this ->extract ($ promise ->result );
139+ }
140+
141+ return $ promise ;
142+ }
143+
144+ private function extract ($ promise )
145+ {
146+ if ($ promise instanceof LazyPromise) {
147+ $ promise = $ promise ->promise ();
148+ }
149+
150+ if ($ promise === $ this ) {
151+ return new RejectedPromise (
152+ new \LogicException ('Cannot resolve a promise with itself. ' )
153+ );
145154 }
146155
147- return $ this -> result ;
156+ return $ promise ;
148157 }
149158
150159 private function call (callable $ callback )
0 commit comments