Return self on then if callback is null

This commit is contained in:
Jake Barnby 2022-05-02 19:43:43 +12:00
parent ab44874626
commit d07fff5c3e
No known key found for this signature in database
GPG key ID: A4674EBC0E404657

View file

@ -30,19 +30,17 @@ class CoroutinePromise
$this->setState(self::STATE_FULFILLED);
};
$reject = function ($value) {
if ($this->isPending()) {
$this->setResult($value);
$this->setState(self::STATE_REJECTED);
}
$this->setResult($value);
$this->setState(self::STATE_REJECTED);
};
go(function (callable $executor, callable $resolve, callable $reject) {
go(function () use ($executor, $resolve, $reject) {
try {
$executor($resolve, $reject);
} catch (\Throwable $exception) {
$reject($exception);
}
}, $executor, $resolve, $reject);
});
}
/**
@ -102,6 +100,12 @@ class CoroutinePromise
*/
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): CoroutinePromise
{
if ($this->isRejected() && $onRejected === null) {
return $this;
}
if ($this->isFulfilled() && $onFulfilled === null) {
return $this;
}
return self::create(function (callable $resolve, callable $reject) use ($onFulfilled, $onRejected) {
while ($this->isPending()) {
usleep(25000);
@ -221,4 +225,14 @@ class CoroutinePromise
{
return $this->state == self::STATE_FULFILLED;
}
/**
* Promise is rejected
*
* @return boolean
*/
final protected function isRejected(): bool
{
return $this->state == self::STATE_REJECTED;
}
}