Skip to content

Commit

Permalink
Add Deferred::isResolved() (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski authored Jul 16, 2021
1 parent 7d4bbc6 commit caa95ed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct()
use Internal\Placeholder {
resolve as public;
fail as public;
isResolved as public;
}
};

Expand Down Expand Up @@ -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();
}
}
8 changes: 8 additions & 0 deletions lib/Internal/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions test/DeferredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion test/MergeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion test/ProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public function testProducerCoroutineThrows(): void
});

while (yield $producer->advance()) {
;
}
$this->fail("The exception thrown from the coroutine should fail the iterator");
});
Expand Down

0 comments on commit caa95ed

Please sign in to comment.