2 namespace GuzzleHttp\Promise;
5 * A promise that has been rejected.
7 * Thenning off of this promise will invoke the onRejected callback
8 * immediately and ignore other callbacks.
10 class RejectedPromise implements PromiseInterface
14 public function __construct($reason)
16 if (method_exists($reason, 'then')) {
17 throw new \InvalidArgumentException(
18 'You cannot create a RejectedPromise with a promise.');
21 $this->reason = $reason;
25 callable $onFulfilled = null,
26 callable $onRejected = null
28 // If there's no onRejected callback then just return self.
34 $reason = $this->reason;
35 $p = new Promise([$queue, 'run']);
36 $queue->add(static function () use ($p, $reason, $onRejected) {
37 if ($p->getState() === self::PENDING) {
39 // Return a resolved promise if onRejected does not throw.
40 $p->resolve($onRejected($reason));
41 } catch (\Throwable $e) {
42 // onRejected threw, so return a rejected promise.
44 } catch (\Exception $e) {
45 // onRejected threw, so return a rejected promise.
54 public function otherwise(callable $onRejected)
56 return $this->then(null, $onRejected);
59 public function wait($unwrap = true, $defaultDelivery = null)
62 throw exception_for($this->reason);
66 public function getState()
68 return self::REJECTED;
71 public function resolve($value)
73 throw new \LogicException("Cannot resolve a rejected promise");
76 public function reject($reason)
78 if ($reason !== $this->reason) {
79 throw new \LogicException("Cannot reject a rejected promise");
83 public function cancel()