Skip to content

Commit 7763182

Browse files
committed
Remove optional $loop argument and always use default loop
1 parent 3422dad commit 7763182

File tree

3 files changed

+36
-64
lines changed

3 files changed

+36
-64
lines changed

README.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,22 @@ Async\await(…);
5454

5555
### await()
5656

57-
The `await(PromiseInterface $promise, ?LoopInterface $loop = null): mixed` function can be used to
57+
The `await(PromiseInterface $promise): mixed` function can be used to
5858
block waiting for the given `$promise` to be fulfilled.
5959

6060
```php
6161
$result = React\Async\await($promise);
6262
```
6363

6464
This function will only return after the given `$promise` has settled, i.e.
65-
either fulfilled or rejected. In the meantime, the event loop will run any
66-
events attached to the same loop until the promise settles.
65+
either fulfilled or rejected.
66+
67+
While the promise is pending, this function will assume control over the event
68+
loop. Internally, it will `run()` the [default loop](https://github.com/reactphp/event-loop#loop)
69+
until the promise settles and then calls `stop()` to terminate execution of the
70+
loop. This means this function is more suited for short-lived promise executions
71+
when using promise-based APIs is not feasible. For long-running applications,
72+
using promise-based APIs by leveraging chained `then()` calls is usually preferable.
6773

6874
Once the promise is fulfilled, this function will return whatever the promise
6975
resolved to.
@@ -83,19 +89,6 @@ try {
8389
}
8490
```
8591

86-
This function takes an optional `LoopInterface|null $loop` parameter that can be used to
87-
pass the event loop instance to use. You can use a `null` value here in order to
88-
use the [default loop](https://github.com/reactphp/event-loop#loop). This value
89-
SHOULD NOT be given unless you're sure you want to explicitly use a given event
90-
loop instance.
91-
92-
Note that this function will assume control over the event loop. Internally, it
93-
will actually `run()` the loop until the promise settles and then calls `stop()` to
94-
terminate execution of the loop. This means this function is more suited for
95-
short-lived promise executions when using promise-based APIs is not feasible.
96-
For long-running applications, using promise-based APIs by leveraging chained
97-
`then()` calls is usually preferable.
98-
9992
### parallel()
10093

10194
The `parallel(array<callable():PromiseInterface<mixed,Exception>> $tasks): PromiseInterface<array<mixed>,Exception>` function can be used

src/functions.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace React\Async;
44

55
use React\EventLoop\Loop;
6-
use React\EventLoop\LoopInterface;
76
use React\Promise\Deferred;
87
use React\Promise\PromiseInterface;
98

@@ -15,8 +14,14 @@
1514
* ```
1615
*
1716
* This function will only return after the given `$promise` has settled, i.e.
18-
* either fulfilled or rejected. In the meantime, the event loop will run any
19-
* events attached to the same loop until the promise settles.
17+
* either fulfilled or rejected.
18+
*
19+
* While the promise is pending, this function will assume control over the event
20+
* loop. Internally, it will `run()` the [default loop](https://github.com/reactphp/event-loop#loop)
21+
* until the promise settles and then calls `stop()` to terminate execution of the
22+
* loop. This means this function is more suited for short-lived promise executions
23+
* when using promise-based APIs is not feasible. For long-running applications,
24+
* using promise-based APIs by leveraging chained `then()` calls is usually preferable.
2025
*
2126
* Once the promise is fulfilled, this function will return whatever the promise
2227
* resolved to.
@@ -36,43 +41,28 @@
3641
* }
3742
* ```
3843
*
39-
* This function takes an optional `LoopInterface|null $loop` parameter that can be used to
40-
* pass the event loop instance to use. You can use a `null` value here in order to
41-
* use the [default loop](https://github.com/reactphp/event-loop#loop). This value
42-
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
43-
* loop instance.
44-
*
45-
* Note that this function will assume control over the event loop. Internally, it
46-
* will actually `run()` the loop until the promise settles and then calls `stop()` to
47-
* terminate execution of the loop. This means this function is more suited for
48-
* short-lived promise executions when using promise-based APIs is not feasible.
49-
* For long-running applications, using promise-based APIs by leveraging chained
50-
* `then()` calls is usually preferable.
51-
*
5244
* @param PromiseInterface $promise
53-
* @param ?LoopInterface $loop
5445
* @return mixed returns whatever the promise resolves to
5546
* @throws \Exception when the promise is rejected
5647
*/
57-
function await(PromiseInterface $promise, LoopInterface $loop = null)
48+
function await(PromiseInterface $promise)
5849
{
5950
$wait = true;
6051
$resolved = null;
6152
$exception = null;
6253
$rejected = false;
63-
$loop = $loop ?: Loop::get();
6454

6555
$promise->then(
66-
function ($c) use (&$resolved, &$wait, $loop) {
56+
function ($c) use (&$resolved, &$wait) {
6757
$resolved = $c;
6858
$wait = false;
69-
$loop->stop();
59+
Loop::stop();
7060
},
71-
function ($error) use (&$exception, &$rejected, &$wait, $loop) {
61+
function ($error) use (&$exception, &$rejected, &$wait) {
7262
$exception = $error;
7363
$rejected = true;
7464
$wait = false;
75-
$loop->stop();
65+
Loop::stop();
7666
}
7767
);
7868

@@ -81,7 +71,7 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
8171
$promise = null;
8272

8373
while ($wait) {
84-
$loop->run();
74+
Loop::run();
8575
}
8676

8777
if ($rejected) {

tests/AwaitTest.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,12 @@
99

1010
class AwaitTest extends TestCase
1111
{
12-
protected $loop;
13-
14-
/**
15-
* @before
16-
*/
17-
public function setUpLoop()
18-
{
19-
$this->loop = Loop::get();
20-
}
21-
2212
public function testAwaitOneRejected()
2313
{
2414
$promise = $this->createPromiseRejected(new \Exception('test'));
2515

2616
$this->setExpectedException('Exception', 'test');
27-
React\Async\await($promise, $this->loop);
17+
React\Async\await($promise);
2818
}
2919

3020
public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
@@ -36,7 +26,7 @@ public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException(
3626
$promise = Promise\reject(false);
3727

3828
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
39-
React\Async\await($promise, $this->loop);
29+
React\Async\await($promise);
4030
}
4131

4232
public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
@@ -48,7 +38,7 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
4838
$promise = Promise\reject(null);
4939

5040
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
51-
React\Async\await($promise, $this->loop);
41+
React\Async\await($promise);
5242
}
5343

5444
/**
@@ -59,7 +49,7 @@ public function testAwaitOneRejectedWithPhp7ErrorWillWrapInUnexpectedValueExcept
5949
$promise = Promise\reject(new \Error('Test', 42));
6050

6151
try {
62-
React\Async\await($promise, $this->loop);
52+
React\Async\await($promise);
6353
$this->fail();
6454
} catch (\UnexpectedValueException $e) {
6555
$this->assertEquals('Promise rejected with unexpected Error: Test', $e->getMessage());
@@ -74,7 +64,7 @@ public function testAwaitOneResolved()
7464
{
7565
$promise = $this->createPromiseResolved(2);
7666

77-
$this->assertEquals(2, React\Async\await($promise, $this->loop));
67+
$this->assertEquals(2, React\Async\await($promise));
7868
}
7969

8070
public function testAwaitReturnsFulfilledValueWithoutGivingLoop()
@@ -89,7 +79,7 @@ public function testAwaitOneInterrupted()
8979
$promise = $this->createPromiseResolved(2, 0.02);
9080
$this->createTimerInterrupt(0.01);
9181

92-
$this->assertEquals(2, React\Async\await($promise, $this->loop));
82+
$this->assertEquals(2, React\Async\await($promise));
9383
}
9484

9585
public function testAwaitOneResolvesShouldNotCreateAnyGarbageReferences()
@@ -101,7 +91,7 @@ public function testAwaitOneResolvesShouldNotCreateAnyGarbageReferences()
10191
gc_collect_cycles();
10292

10393
$promise = Promise\resolve(1);
104-
React\Async\await($promise, $this->loop);
94+
React\Async\await($promise);
10595
unset($promise);
10696

10797
$this->assertEquals(0, gc_collect_cycles());
@@ -117,7 +107,7 @@ public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
117107

118108
$promise = Promise\reject(new \RuntimeException());
119109
try {
120-
React\Async\await($promise, $this->loop);
110+
React\Async\await($promise);
121111
} catch (\Exception $e) {
122112
// no-op
123113
}
@@ -140,7 +130,7 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
140130

141131
$promise = Promise\reject(null);
142132
try {
143-
React\Async\await($promise, $this->loop);
133+
React\Async\await($promise);
144134
} catch (\Exception $e) {
145135
// no-op
146136
}
@@ -153,7 +143,7 @@ protected function createPromiseResolved($value = null, $delay = 0.01)
153143
{
154144
$deferred = new Deferred();
155145

156-
$this->loop->addTimer($delay, function () use ($deferred, $value) {
146+
Loop::addTimer($delay, function () use ($deferred, $value) {
157147
$deferred->resolve($value);
158148
});
159149

@@ -164,7 +154,7 @@ protected function createPromiseRejected($value = null, $delay = 0.01)
164154
{
165155
$deferred = new Deferred();
166156

167-
$this->loop->addTimer($delay, function () use ($deferred, $value) {
157+
Loop::addTimer($delay, function () use ($deferred, $value) {
168158
$deferred->reject($value);
169159
});
170160

@@ -173,9 +163,8 @@ protected function createPromiseRejected($value = null, $delay = 0.01)
173163

174164
protected function createTimerInterrupt($delay = 0.01)
175165
{
176-
$loop = $this->loop;
177-
$loop->addTimer($delay, function () use ($loop) {
178-
$loop->stop();
166+
Loop::addTimer($delay, function () {
167+
Loop::stop();
179168
});
180169
}
181170

0 commit comments

Comments
 (0)