diff --git a/lib/Deferred.php b/lib/Deferred.php index 39e47acc..cec1c5a4 100644 --- a/lib/Deferred.php +++ b/lib/Deferred.php @@ -23,6 +23,7 @@ public function __construct() use Internal\Placeholder { resolve as public; fail as public; + isResolved as public; } }; @@ -64,4 +65,12 @@ public function fail(\Throwable $reason) /** @psalm-suppress UndefinedInterfaceMethod */ $this->resolver->fail($reason); } + + /** + * @return bool True if the promise has been resolved. + */ + public function isResolved(): bool + { + return $this->resolver->isResolved(); + } } diff --git a/lib/Internal/Placeholder.php b/lib/Internal/Placeholder.php index b84aa704..dccf6067 100644 --- a/lib/Internal/Placeholder.php +++ b/lib/Internal/Placeholder.php @@ -176,4 +176,12 @@ private function fail(\Throwable $reason) { $this->resolve(new Failure($reason)); } + + /** + * @return bool True if the placeholder has been resolved. + */ + private function isResolved(): bool + { + return $this->resolved; + } } diff --git a/test/DeferredTest.php b/test/DeferredTest.php index 4664a437..da1064aa 100644 --- a/test/DeferredTest.php +++ b/test/DeferredTest.php @@ -35,8 +35,11 @@ public function testResolve() $result = $value; }); + $this->assertFalse($this->deferred->isResolved()); + $this->deferred->resolve($value); + $this->assertTrue($this->deferred->isResolved()); $this->assertTrue($invoked); $this->assertSame($value, $result); } @@ -55,8 +58,11 @@ public function testFail() $result = $exception; }); + $this->assertFalse($this->deferred->isResolved()); + $this->deferred->fail($exception); + $this->assertTrue($this->deferred->isResolved()); $this->assertTrue($invoked); $this->assertSame($exception, $result); } diff --git a/test/MergeTest.php b/test/MergeTest.php index aed6df62..c5bcebe3 100644 --- a/test/MergeTest.php +++ b/test/MergeTest.php @@ -87,7 +87,6 @@ public function testMergeWithFailedIterator(): void try { while (yield $iterator->advance()) { - ; } $this->fail("The exception used to fail the iterator should be thrown from advance()"); } catch (TestException $reason) { diff --git a/test/ProducerTest.php b/test/ProducerTest.php index 1e567816..2719ac90 100644 --- a/test/ProducerTest.php +++ b/test/ProducerTest.php @@ -117,7 +117,6 @@ public function testProducerCoroutineThrows(): void }); while (yield $producer->advance()) { - ; } $this->fail("The exception thrown from the coroutine should fail the iterator"); });