2 namespace GuzzleHttp\Promise;
5 * A promise that has been fulfilled.
7 * Thenning off of this promise will invoke the onFulfilled callback
8 * immediately and ignore other callbacks.
10 class FulfilledPromise implements PromiseInterface
14 public function __construct($value)
16 if (method_exists($value, 'then')) {
17 throw new \InvalidArgumentException(
18 'You cannot create a FulfilledPromise with a promise.');
21 $this->value = $value;
25 callable $onFulfilled = null,
26 callable $onRejected = null
28 // Return itself if there is no onFulfilled function.
34 $p = new Promise([$queue, 'run']);
35 $value = $this->value;
36 $queue->add(static function () use ($p, $value, $onFulfilled) {
37 if ($p->getState() === self::PENDING) {
39 $p->resolve($onFulfilled($value));
40 } catch (\Throwable $e) {
42 } catch (\Exception $e) {
51 public function otherwise(callable $onRejected)
53 return $this->then(null, $onRejected);
56 public function wait($unwrap = true, $defaultDelivery = null)
58 return $unwrap ? $this->value : null;
61 public function getState()
63 return self::FULFILLED;
66 public function resolve($value)
68 if ($value !== $this->value) {
69 throw new \LogicException("Cannot resolve a fulfilled promise");
73 public function reject($reason)
75 throw new \LogicException("Cannot reject a fulfilled promise");
78 public function cancel()