-
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathRejectedPromise.php
More file actions
89 lines (74 loc) · 2.49 KB
/
RejectedPromise.php
File metadata and controls
89 lines (74 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace React\Promise;
final class RejectedPromise implements PromiseInterface
{
private $reason;
public function __construct($reason)
{
if (!$reason instanceof \Throwable && !$reason instanceof \Exception) {
throw new \InvalidArgumentException(
sprintf(
'A Promise must be rejected with a \Throwable or \Exception instance, got "%s" instead.',
is_object($reason) ? get_class($reason) : gettype($reason)
)
);
}
$this->reason = $reason;
}
public function then(callable $onFulfilled = null, callable $onRejected = null)
{
if (null === $onRejected) {
return $this;
}
return new Promise(function (callable $resolve, callable $reject) use ($onRejected) {
enqueue(function () use ($resolve, $reject, $onRejected) {
try {
$resolve($onRejected($this->reason));
} catch (\Throwable $exception) {
$reject($exception);
} catch (\Exception $exception) {
$reject($exception);
}
});
});
}
public function done(callable $onFulfilled = null, callable $onRejected = null)
{
enqueue(function () use ($onRejected) {
if (null === $onRejected) {
return fatalError($this->reason);
}
try {
$result = $onRejected($this->reason);
} catch (\Throwable $exception) {
return fatalError($exception);
} catch (\Exception $exception) {
return fatalError($exception);
}
if ($result instanceof self) {
return fatalError($result->reason);
}
if ($result instanceof PromiseInterface) {
$result->done();
}
});
}
public function otherwise(callable $onRejected)
{
if (!_checkTypehint($onRejected, $this->reason)) {
return $this;
}
return $this->then(null, $onRejected);
}
public function always(callable $onFulfilledOrRejected)
{
return $this->then(null, function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return new RejectedPromise($reason);
});
});
}
public function cancel()
{
}
}