friendship ended with social-app. php is my new best friend
1<?php
2
3namespace React\Promise\Internal;
4
5use React\Promise\PromiseInterface;
6use function React\Promise\resolve;
7
8/**
9 * @internal
10 *
11 * @template T
12 * @template-implements PromiseInterface<T>
13 */
14final class FulfilledPromise implements PromiseInterface
15{
16 /** @var T */
17 private $value;
18
19 /**
20 * @param T $value
21 * @throws \InvalidArgumentException
22 */
23 public function __construct($value = null)
24 {
25 if ($value instanceof PromiseInterface) {
26 throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.');
27 }
28
29 $this->value = $value;
30 }
31
32 /**
33 * @template TFulfilled
34 * @param ?(callable((T is void ? null : T)): (PromiseInterface<TFulfilled>|TFulfilled)) $onFulfilled
35 * @return PromiseInterface<($onFulfilled is null ? T : TFulfilled)>
36 */
37 public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface
38 {
39 if (null === $onFulfilled) {
40 return $this;
41 }
42
43 try {
44 /**
45 * @var PromiseInterface<T>|T $result
46 */
47 $result = $onFulfilled($this->value);
48 return resolve($result);
49 } catch (\Throwable $exception) {
50 return new RejectedPromise($exception);
51 }
52 }
53
54 public function catch(callable $onRejected): PromiseInterface
55 {
56 return $this;
57 }
58
59 public function finally(callable $onFulfilledOrRejected): PromiseInterface
60 {
61 return $this->then(function ($value) use ($onFulfilledOrRejected): PromiseInterface {
62 /** @var T $value */
63 return resolve($onFulfilledOrRejected())->then(function () use ($value) {
64 return $value;
65 });
66 });
67 }
68
69 public function cancel(): void
70 {
71 }
72
73 /**
74 * @deprecated 3.0.0 Use `catch()` instead
75 * @see self::catch()
76 */
77 public function otherwise(callable $onRejected): PromiseInterface
78 {
79 return $this->catch($onRejected);
80 }
81
82 /**
83 * @deprecated 3.0.0 Use `finally()` instead
84 * @see self::finally()
85 */
86 public function always(callable $onFulfilledOrRejected): PromiseInterface
87 {
88 return $this->finally($onFulfilledOrRejected);
89 }
90}