@@ -36,46 +36,38 @@ class Future {
36
36
public $ value ;
37
37
public $ reason ;
38
38
private $ subscribers = array ();
39
- public static $ nextTick = null ;
40
39
41
40
public function __construct ($ computation = NULL ) {
42
41
if (is_callable ($ computation )) {
43
- $ self = $ this ;
44
- $ nextTick = self ::$ nextTick ;
45
- $ nextTick (function () use ($ self , $ computation ) {
46
- try {
47
- $ self ->resolve (call_user_func ($ computation ));
48
- }
49
- catch (UncatchableException $ e ) {
50
- throw $ e ->getPrevious ();
51
- }
52
- catch (Exception $ e ) {
53
- $ self ->reject ($ e );
54
- }
55
- catch (Throwable $ e ) {
56
- $ self ->reject ($ e );
57
- }
58
- });
59
- }
60
- }
61
-
62
- private function privateCall ($ callback , $ next , $ x ) {
63
- $ nextTick = self ::$ nextTick ;
64
- $ nextTick (function () use ($ callback , $ next , $ x ) {
65
42
try {
66
- $ r = call_user_func ($ callback , $ x );
67
- $ next ->resolve ($ r );
43
+ $ this ->resolve (call_user_func ($ computation ));
68
44
}
69
45
catch (UncatchableException $ e ) {
70
46
throw $ e ->getPrevious ();
71
47
}
72
48
catch (Exception $ e ) {
73
- $ next ->reject ($ e );
49
+ $ this ->reject ($ e );
74
50
}
75
51
catch (Throwable $ e ) {
76
- $ next ->reject ($ e );
52
+ $ this ->reject ($ e );
77
53
}
78
- });
54
+ }
55
+ }
56
+
57
+ private function privateCall ($ callback , $ next , $ x ) {
58
+ try {
59
+ $ r = call_user_func ($ callback , $ x );
60
+ $ next ->resolve ($ r );
61
+ }
62
+ catch (UncatchableException $ e ) {
63
+ throw $ e ->getPrevious ();
64
+ }
65
+ catch (Exception $ e ) {
66
+ $ next ->reject ($ e );
67
+ }
68
+ catch (Throwable $ e ) {
69
+ $ next ->reject ($ e );
70
+ }
79
71
}
80
72
81
73
private function privateResolve ($ onfulfill , $ next , $ x ) {
@@ -96,7 +88,7 @@ private function privateReject($onreject, $next, $e) {
96
88
}
97
89
}
98
90
99
- public function resolve ($ value ) {
91
+ public function resolve ($ value = NULL ) {
100
92
if ($ value === $ this ) {
101
93
$ this ->reject (new TypeError ('Self resolution ' ));
102
94
return ;
@@ -193,10 +185,7 @@ public function then($onfulfill, $onreject = NULL) {
193
185
194
186
public function done ($ onfulfill , $ onreject = NULL ) {
195
187
$ this ->then ($ onfulfill , $ onreject )->then (NULL , function ($ error ) {
196
- $ nextTick = self ::$ nextTick ;
197
- $ nextTick (function () use ($ error ) {
198
- throw new UncatchableException ("" , 0 , $ error );
199
- });
188
+ throw new UncatchableException ("" , 0 , $ error );
200
189
});
201
190
}
202
191
@@ -242,7 +231,8 @@ function($e) use ($action) {
242
231
);
243
232
}
244
233
245
- public function complete ($ oncomplete ) {
234
+ public function complete ($ oncomplete = false ) {
235
+ $ oncomplete = $ oncomplete ?: function ($ v ) { return $ v ; };
246
236
return $ this ->then ($ oncomplete , $ oncomplete );
247
237
}
248
238
0 commit comments