Chris@0: reason = $reason; Chris@0: } Chris@0: Chris@0: public function then( Chris@0: callable $onFulfilled = null, Chris@0: callable $onRejected = null Chris@0: ) { Chris@0: // If there's no onRejected callback then just return self. Chris@0: if (!$onRejected) { Chris@0: return $this; Chris@0: } Chris@0: Chris@0: $queue = queue(); Chris@0: $reason = $this->reason; Chris@0: $p = new Promise([$queue, 'run']); Chris@0: $queue->add(static function () use ($p, $reason, $onRejected) { Chris@0: if ($p->getState() === self::PENDING) { Chris@0: try { Chris@0: // Return a resolved promise if onRejected does not throw. Chris@0: $p->resolve($onRejected($reason)); Chris@0: } catch (\Throwable $e) { Chris@0: // onRejected threw, so return a rejected promise. Chris@0: $p->reject($e); Chris@0: } catch (\Exception $e) { Chris@0: // onRejected threw, so return a rejected promise. Chris@0: $p->reject($e); Chris@0: } Chris@0: } Chris@0: }); Chris@0: Chris@0: return $p; Chris@0: } Chris@0: Chris@0: public function otherwise(callable $onRejected) Chris@0: { Chris@0: return $this->then(null, $onRejected); Chris@0: } Chris@0: Chris@0: public function wait($unwrap = true, $defaultDelivery = null) Chris@0: { Chris@0: if ($unwrap) { Chris@0: throw exception_for($this->reason); Chris@0: } Chris@0: } Chris@0: Chris@0: public function getState() Chris@0: { Chris@0: return self::REJECTED; Chris@0: } Chris@0: Chris@0: public function resolve($value) Chris@0: { Chris@0: throw new \LogicException("Cannot resolve a rejected promise"); Chris@0: } Chris@0: Chris@0: public function reject($reason) Chris@0: { Chris@0: if ($reason !== $this->reason) { Chris@0: throw new \LogicException("Cannot reject a rejected promise"); Chris@0: } Chris@0: } Chris@0: Chris@0: public function cancel() Chris@0: { Chris@0: // pass Chris@0: } Chris@0: }