diff --git a/README.md b/README.md index e3171348..befa1cd3 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ Table of Contents * [Promise](#promise-2) * [FulfilledPromise](#fulfilledpromise) * [RejectedPromise](#rejectedpromise) - * [LazyPromise](#lazypromise) * [Functions](#functions) * [resolve()](#resolve) * [reject()](#reject) @@ -156,7 +155,6 @@ Neither its state nor its result (or error) can be modified. * [Promise](#promise-2) * [FulfilledPromise](#fulfilledpromise) * [RejectedPromise](#rejectedpromise) -* [LazyPromise](#lazypromise) #### PromiseInterface::then() @@ -361,27 +359,6 @@ $promise = React\Promise\RejectedPromise($reason); Note, that `$reason` **cannot** be a promise. It's recommended to use [reject()](#reject) for creating rejected promises. -### LazyPromise - -Creates a promise which will be lazily initialized by `$factory` once a consumer -calls the `then()` method. - -```php -$factory = function () { - $deferred = new React\Promise\Deferred(); - - // Do some heavy stuff here and resolve the deferred once completed - - return $deferred->promise(); -}; - -$promise = new React\Promise\LazyPromise($factory); - -// $factory will only be executed once we call then() -$promise->then(function ($value) { -}); -``` - ### Functions Useful functions for creating, joining, mapping and reducing collections of diff --git a/src/LazyPromise.php b/src/LazyPromise.php deleted file mode 100644 index 95573406..00000000 --- a/src/LazyPromise.php +++ /dev/null @@ -1,61 +0,0 @@ -factory = $factory; - } - - public function then(callable $onFulfilled = null, callable $onRejected = null) - { - return $this->promise()->then($onFulfilled, $onRejected); - } - - public function done(callable $onFulfilled = null, callable $onRejected = null) - { - return $this->promise()->done($onFulfilled, $onRejected); - } - - public function otherwise(callable $onRejected) - { - return $this->promise()->otherwise($onRejected); - } - - public function always(callable $onFulfilledOrRejected) - { - return $this->promise()->always($onFulfilledOrRejected); - } - - public function cancel() - { - return $this->promise()->cancel(); - } - - /** - * @internal - * @see Promise::settle() - */ - public function promise() - { - if (null === $this->promise) { - $factory = $this->factory; - $this->factory = null; - - try { - $this->promise = resolve($factory()); - } catch (\Throwable $exception) { - $this->promise = new RejectedPromise($exception); - } catch (\Exception $exception) { - $this->promise = new RejectedPromise($exception); - } - } - - return $this->promise; - } -} diff --git a/src/Promise.php b/src/Promise.php index 8a290301..09672c11 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -132,19 +132,8 @@ private function settle(PromiseInterface $result) private function unwrap($promise) { - $promise = $this->extract($promise); - while ($promise instanceof self && null !== $promise->result) { - $promise = $this->extract($promise->result); - } - - return $promise; - } - - private function extract($promise) - { - if ($promise instanceof LazyPromise) { - $promise = $promise->promise(); + $promise = $promise->result; } if ($promise === $this) { diff --git a/tests/LazyPromiseTest.php b/tests/LazyPromiseTest.php deleted file mode 100644 index e894ebd2..00000000 --- a/tests/LazyPromiseTest.php +++ /dev/null @@ -1,96 +0,0 @@ -promise(); - }; - - return new CallbackPromiseAdapter([ - 'promise' => function () use ($factory) { - return new LazyPromise($factory); - }, - 'resolve' => [$d, 'resolve'], - 'reject' => [$d, 'reject'], - 'settle' => [$d, 'resolve'], - ]); - } - - /** @test */ - public function shouldNotCallFactoryIfThenIsNotInvoked() - { - new LazyPromise($this->expectCallableNever()); - } - - /** @test */ - public function shouldCallFactoryIfThenIsInvoked() - { - $p = new LazyPromise($this->expectCallableOnce()); - $p->then(); - } - - /** @test */ - public function shouldReturnPromiseFromFactory() - { - $factory = $this->createCallableMock(); - $factory - ->expects($this->once()) - ->method('__invoke') - ->will($this->returnValue(new FulfilledPromise(1))); - - $onFulfilled = $this->createCallableMock(); - $onFulfilled - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo(1)); - - $p = new LazyPromise($factory); - - $p->then($onFulfilled); - } - - /** @test */ - public function shouldReturnPromiseIfFactoryReturnsNull() - { - $factory = $this->createCallableMock(); - $factory - ->expects($this->once()) - ->method('__invoke') - ->will($this->returnValue(null)); - - $p = new LazyPromise($factory); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $p->then()); - } - - /** @test */ - public function shouldReturnRejectedPromiseIfFactoryThrowsException() - { - $exception = new \Exception(); - - $factory = $this->createCallableMock(); - $factory - ->expects($this->once()) - ->method('__invoke') - ->will($this->throwException($exception)); - - $onRejected = $this->createCallableMock(); - $onRejected - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo($exception)); - - $p = new LazyPromise($factory); - - $p->then($this->expectCallableNever(), $onRejected); - } -}