-
Notifications
You must be signed in to change notification settings - Fork 0
/
Promise.php
91 lines (59 loc) · 1.24 KB
/
Promise.php
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
90
91
<?php
class Promise
{
/*
TODO:
1. Promise states: Pending > Rejected (error) or Fullfiled (resolved)
2. Make it works for WEB and CLI
Or check in the PHP file whether it's called from the command line or not:
if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}
3. Make it chainable for promises (?)
4. https://stackoverflow.com/questions/16596281/is-this-implementation-a-fair-example-of-a-promise-in-php
*/
private $callbacks = [];
private $last_status;
public function __construct()
{
}
public function then( callable $onFullfiled, callable $onRejected )
{
$this->setCallback( $onFullfiled, $onRejected );
return $this;
}
public function thenOtherwise( callable $onFullfiled, callable $onRejected )
{
$this->setCallback( $onFullfiled, $onRejected );
return $this;
}
public function resolve()
{
}
private function pending()
{
}
private function reject()
{
}
public function setCallback( callable $onFullfiled, callable $onRejected )
{
$this->callbacks[] = [
'onFullfiled' => $onFullfiled,
'onRejected' => $onRejected,
];
}
}
$promise = new Promise();
$promise
->then(
function()
{
},
function ()
{
}
)
->resolve();