mirror of
https://github.com/appwrite/appwrite
synced 2026-05-23 17:08:45 +00:00
Return self on then if callback is null
This commit is contained in:
parent
ab44874626
commit
d07fff5c3e
1 changed files with 20 additions and 6 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue