File tree 3 files changed +43
-50
lines changed
3 files changed +43
-50
lines changed Original file line number Diff line number Diff line change @@ -42,15 +42,12 @@ public function then(callable $onFulfilled): self
42
42
/**
43
43
* Adds a rejection handler to the promise and returns $this for chaining.
44
44
*
45
- * The catch() method is analogous to otherwise() in ReactPHP promises.
46
- *
47
45
* @param callable $onRejected The callback to execute if the promise rejects.
48
46
* @return $this
49
47
*/
50
48
public function catch (callable $ onRejected ): self
51
49
{
52
- // ReactPHP doesn't have a catch() method, but otherwise() attaches an error handler.
53
- $ this ->promise = $ this ->promise ->otherwise ($ onRejected );
50
+ $ this ->promise = $ this ->promise ->catch ($ onRejected );
54
51
55
52
return $ this ;
56
53
}
Original file line number Diff line number Diff line change 3
3
declare (strict_types=1 );
4
4
5
5
use Matrix \AsyncProcessManager ;
6
- use React \EventLoop \Loop ;
7
- use React \Promise \PromiseInterface ;
8
6
9
7
uses ()->group ('manager ' , 'async ' );
10
8
11
- /**
12
- * Runs the event loop until the given promise settles or a timeout is reached.
13
- *
14
- * @param PromiseInterface<mixed, \Throwable> $promise The promise to await.
15
- * @param float $timeout Maximum time in seconds to wait.
16
- * @return mixed The resolved value of the promise.
17
- *
18
- * @throws \Throwable If the promise rejects.
19
- */
20
- function awaitPromise (PromiseInterface $ promise , float $ timeout = 2.0 )
21
- {
22
- $ resolved = false ;
23
- $ rejected = false ;
24
- $ result = null ;
25
- $ error = null ;
26
-
27
- $ promise ->then (
28
- function ($ val ) use (&$ resolved , &$ result ) {
29
- $ resolved = true ;
30
- $ result = $ val ;
31
- Loop::stop ();
32
- },
33
- function ($ err ) use (&$ rejected , &$ error ) {
34
- $ rejected = true ;
35
- $ error = $ err ;
36
- Loop::stop ();
37
- }
38
- );
39
-
40
- Loop::addTimer ($ timeout , function () {
41
- Loop::stop ();
42
- });
43
-
44
- Loop::run ();
45
-
46
- if ($ rejected ) {
47
- throw $ error ;
48
- }
49
-
50
- return $ result ;
51
- }
52
-
53
9
it ('resolves with the child result when the callable succeeds ' , function () {
54
10
$ manager = new AsyncProcessManager ;
55
11
$ promise = $ manager ->fork (function () {
Original file line number Diff line number Diff line change 2
2
3
3
declare (strict_types=1 );
4
4
5
+ use React \EventLoop \Loop ;
6
+ use React \Promise \PromiseInterface ;
7
+
5
8
/*
6
9
|--------------------------------------------------------------------------
7
10
| Test Case
41
44
|
42
45
*/
43
46
44
- function something ()
47
+ /**
48
+ * Runs the event loop until the given promise settles or a timeout is reached.
49
+ *
50
+ * @param PromiseInterface<mixed, \Throwable> $promise The promise to await.
51
+ * @param float $timeout Maximum time in seconds to wait.
52
+ * @return mixed The resolved value of the promise.
53
+ *
54
+ * @throws \Throwable If the promise rejects.
55
+ */
56
+ function awaitPromise (PromiseInterface $ promise , float $ timeout = 2.0 )
45
57
{
46
- // ..
58
+ $ resolved = false ;
59
+ $ rejected = false ;
60
+ $ result = null ;
61
+ $ error = null ;
62
+
63
+ $ promise ->then (
64
+ function ($ val ) use (&$ resolved , &$ result ) {
65
+ $ resolved = true ;
66
+ $ result = $ val ;
67
+ Loop::stop ();
68
+ },
69
+ function ($ err ) use (&$ rejected , &$ error ) {
70
+ $ rejected = true ;
71
+ $ error = $ err ;
72
+ Loop::stop ();
73
+ }
74
+ );
75
+
76
+ Loop::addTimer ($ timeout , function () {
77
+ Loop::stop ();
78
+ });
79
+
80
+ Loop::run ();
81
+
82
+ if ($ rejected ) {
83
+ throw $ error ;
84
+ }
85
+
86
+ return $ result ;
47
87
}
You can’t perform that action at this time.
0 commit comments