PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / react / Promise / RejectedPromise.php

RejectedPromise.php in DecaLog 4.4.0, at includes/libraries/react/Promise/RejectedPromise.php

77 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace React\Promise;
4
5 class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
6 {
7 private $reason;
8
9 public function __construct($reason = null)
10 {
11 if ($reason instanceof PromiseInterface) {
12 throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.');
13 }
14
15 $this->reason = $reason;
16 }
17
18 public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
19 {
20 if (null === $onRejected) {
21 return $this;
22 }
23
24 try {
25 return resolve($onRejected($this->reason));
26 } catch (\Throwable $exception) {
27 return new RejectedPromise($exception);
28 } catch (\Exception $exception) {
29 return new RejectedPromise($exception);
30 }
31 }
32
33 public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
34 {
35 if (null === $onRejected) {
36 throw UnhandledRejectionException::resolve($this->reason);
37 }
38
39 $result = $onRejected($this->reason);
40
41 if ($result instanceof self) {
42 throw UnhandledRejectionException::resolve($result->reason);
43 }
44
45 if ($result instanceof ExtendedPromiseInterface) {
46 $result->done();
47 }
48 }
49
50 public function otherwise(callable $onRejected)
51 {
52 if (!_checkTypehint($onRejected, $this->reason)) {
53 return $this;
54 }
55
56 return $this->then(null, $onRejected);
57 }
58
59 public function always(callable $onFulfilledOrRejected)
60 {
61 return $this->then(null, function ($reason) use ($onFulfilledOrRejected) {
62 return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
63 return new RejectedPromise($reason);
64 });
65 });
66 }
67
68 public function progress(callable $onProgress)
69 {
70 return $this;
71 }
72
73 public function cancel()
74 {
75 }
76 }
77