12 public readonly
string $id;
13 private State $state = State::Pending;
14 private ?Closure $onFulfilled =
null;
15 private ?Closure $onRejected =
null;
23 $job([$this,
'fulfill'], [$this,
'reject']);
25 catch (Throwable $exception)
31 abstract public function wait(): mixed;
33 abstract public function onWait(): mixed;
42 return $this->result ??
null;
45 public function then(?callable $onFulfilled =
null, ?callable $onRejected =
null): static
47 return new static(
function(callable $fulfill, callable $reject) use ($onFulfilled, $onRejected):
void {
48 $wrappedOnFulfilled = $this->wrapCompletionHandler($onFulfilled, $fulfill, $reject);
49 $wrappedOnRejected = $this->wrapCompletionHandler($onRejected, $fulfill, $reject);
51 if ($this->state === State::Pending)
53 $this->onFulfilled = $wrappedOnFulfilled;
54 $this->onRejected = $wrappedOnRejected;
56 elseif ($this->state === State::Fulfilled)
58 $wrappedOnFulfilled();
67 public function catch(callable $onRejected):
static
69 return $this->
then(
null, $onRejected);
84 if ($this->state !== State::Pending)
89 $callback = $newState === State::Fulfilled ? $this->onFulfilled : $this->onRejected;
90 $this->state = $newState;
102 private function wrapCompletionHandler(?callable $completionHandler, callable $fulfill, callable $reject): callable
104 return function() use ($completionHandler, $fulfill, $reject):
void {
107 $this->processCompletion($completionHandler, $fulfill, $reject);
116 private function processCompletion(?callable $completionHandler, callable $fulfill, callable $reject): void
118 if ($completionHandler)
120 $this->executeCompletionHandler($completionHandler, $fulfill, $reject);
124 $this->propagateState($fulfill, $reject);
128 private function executeCompletionHandler(callable $completionHandler, callable $fulfill, callable $reject): void
130 $result = $completionHandler($this->result);
134 $result->then($fulfill, $reject);
142 private function propagateState(callable $fulfill, callable $reject): void
144 if ($this->state === State::Fulfilled)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)