8000 Merge pull request #45 from clue-labs/docs · clue/reactphp-block@942a887 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 942a887

Browse files
authored
Merge pull request #45 from clue-labs/docs
Improve API documentation
2 parents 2f516b2 + d357497 commit 942a887

File tree

2 files changed

+164
-86
lines changed

2 files changed

+164
-86
lines changed

README.md

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,119 +88,134 @@ $loop = React\EventLoop\Factory::create();
8888

8989
### sleep()
9090

91-
The `sleep($seconds, LoopInterface $loop)` method can be used to wait/sleep for $time seconds.
91+
The `sleep($seconds, LoopInterface $loop): void` function can be used to
92+
wait/sleep for `$time` seconds.
9293

9394
```php
9495
Block\sleep(1.5, $loop);
9596
```
9697

97-
The $time value will be used as a timer for the loop so that it keeps running
98-
until the timeout triggers.
99-
This implies that if you pass a really small (or negative) value, it will still
100-
start a timer and will thus trigger at the earliest possible time in the future.
98+
This function will only return after the given `$time` has elapsed. In the
99+
meantime, the event loop will run any other events attached to the same loop
100+
until the timer fires. If there are no other events attached to this loop,
101+
it will behave similar to the built-in [`sleep()`](https://www.php.net/manual/en/function.sleep.php).
101102

102-
While this may look similar to PHP's [`sleep()`](https://www.php.net/sleep) function,
103-
it's actual way more powerful:
104-
Instead of making the whole process sleep and handing over control to your operating system,
105-
this function actually executes the loop in the meantime.
106-
This is particularly useful if you've attached more async tasks to the same loop instance.
107-
If there are no other (async) tasks, this will behave similar to `sleep()`.
103+
Internally, the `$time` argument will be used as a timer for the loop so that
104+
it keeps running until this timer triggers. This implies that if you pass a
105+
really small (or negative) value, it will still start a timer and will thus
106+
trigger at the earliest possible time in the future.
108107

109108
### await()
110109

111-
The `await(PromiseInterface $promise, LoopInterface $loop, $timeout = null)`
112-
function can be used to block waiting for the given $promise to resolve.
110+
The `await(PromiseInterface $promise, LoopInterface $loop, ?float $timeout = null): mixed` function can be used to
111+
Block waiting for the given `$promise` to be fulfilled.
113112

114113
```php
115-
$result = Block\await($promise, $loop);
114+
$result = Block\await($promise, $loop, $timeout);
116115
```
117116

118-
Once the promise is resolved, this will return whatever the promise resolves to.
117+
This function will only return after the given `$promise` has settled, i.e.
118+
either fulfilled or rejected. In the meantime, the event loop will run any
119+
events attached to the same loop until the promise settles.
120+
121+
Once the promise is fulfilled, this function will return whatever the promise
122+
resolved to.
119123

120-
Once the promise is rejected, this will throw whatever the promise rejected with.
121-
If the promise did not reject with an `Exception`, then this function will
122-
throw an `UnexpectedValueException` instead.
124+
Once the promise is rejected, this will throw whatever the promise rejected
125+
with. If the promise did not reject with an `Exception`, then this function
126+
will throw an `UnexpectedValueException` instead.
123127

124128
```php
125129
try {
126-
$value = Block\await($promise, $loop);
130+
$result = Block\await($promise, $loop);
127131
// promise successfully fulfilled with $value
128-
echo 'Result: ' . $value;
132+
echo 'Result: ' . $result;
129133
} catch (Exception $exception) {
130134
// promise rejected with $exception
131135
echo 'ERROR: ' . $exception->getMessage();
132136
}
133137
```
134138

135-
If no $timeout is given and the promise stays pending, then this will
136-
potentially wait/block forever until the promise is settled.
139+
If no `$timeout` argument is given and the promise stays pending, then this
140+
will potentially wait/block forever until the promise is settled.
137141

138-
If a $timeout is given and the promise is still pending once the timeout
139-
triggers, this will `cancel()` the promise and throw a `TimeoutException`.
142+
If a `$timeout` argument is given and the promise is still pending once the
143+
timeout triggers, this will `cancel()` the promise and throw a `TimeoutException`.
140144
This implies that if you pass a really small (or negative) value, it will still
141145
start a timer and will thus trigger at the earliest possible time in the future.
142146

143147
### awaitAny()
144148

145-
The `awaitAny(array $promises, LoopInterface $loop, $timeout = null)`
146-
function can be used to wait for ANY of the given promises to resolve.
149+
The `awaitAny(PromiseInterface[] $promises, LoopInterface $loop, ?float $timeout = null): mixed` function can be used to
150+
wait for ANY of the given promises to be fulfilled.
147151

148152
```php
149153
$promises = array(
150154
$promise1,
151155
$promise2
152156
);
153157

154-
$firstResult = Block\awaitAny($promises, $loop);
158+
$firstResult = Block\awaitAny($promises, $loop, $timeout);
155159

156160
echo 'First result: ' . $firstResult;
157161
```
158162

159-
Once the first promise is resolved, this will try to `cancel()` all
160-
remaining promises and return whatever the first promise resolves to.
163+
This function will only return after ANY of the given `$promises` has been
164+
fulfilled or will throw when ALL of them have been rejected. In the meantime,
165+
the event loop will run any events attached to the same loop.
161166

162-
If ALL promises fail to resolve, this will fail and throw an `Exception`.
167+
Once ANY promise is fulfilled, this function will return whatever this
168+
promise resolved to and will try to `cancel()` all remaining promises.
163169

164-
If no $timeout is given and either promise stays pending, then this will
165-
potentially wait/block forever until the first promise is settled.
170+
Once ALL promises reject, this function will fail and throw an `UnderflowException`.
171+
Likewise, this will throw if an empty array of `$promises` is passed.
166172

167-
If a $timeout is given and either promise is still pending once the timeout
168-
triggers, this will `cancel()` all pending promises and throw a `TimeoutException`.
169-
This implies that if you pass a really small (or negative) value, it will still
170-
start a timer and will thus trigger at the earliest possible time in the future.
173+
If no `$timeout` argument is given and ALL promises stay pending, then this
174+
will potentially wait/block forever until the promise is fulfilled.
175+
176+
If a `$timeout` argument is given and ANY promises are still pending once
177+
the timeout triggers, this will `cancel()` all pending promises and throw a
178+
`TimeoutException`. This implies that if you pass a really small (or negative)
179+
value, it will still start a timer and will thus trigger at the earliest
180+
possible time in the future.
171181

172182
### awaitAll()
173183

174-
The `awaitAll(array $promises, LoopInterface $loop, $timeout = null)`
175-
function can be used to wait for ALL of the given promises to resolve.
184+
The `awaitAll(PromiseInterface[] $promises, LoopInterface $loop, ?float $timeout = null): mixed[]` function can be used to
185+
wait for ALL of the given promises to be fulfilled.
176186

177187
```php
178188
$promises = array(
179189
$promise1,
180190
$promise2
181191
);
182192

183-
$allResults = Block\awaitAll($promises, $loop);
193+
$allResults = Block\awaitAll($promises, $loop, $timeout);
184194

185195
echo 'First promise resolved with: ' . $allResults[0];
186196
```
187197

188-
Once the last promise resolves, this will return an array with whatever
198+
This function will only return after ALL of the given `$promises` have been
199+
fulfilled or will throw when ANY of them have been rejected. In the meantime,
200+
the event loop will run any events attached to the same loop.
201+
202+
Once ALL promises are fulfilled, this will return an array with whatever
189203
each promise resolves to. Array keys will be left intact, i.e. they can
190204
be used to correlate the return array to the promises passed.
205+
Likewise, this will return an empty array if an empty array of `$promises` is passed.
191206

192-
If ANY promise fails to resolve, this will try to `cancel()` all
193-
remaining promises and throw an `Exception`.
194-
If the promise did not reject with an `Exception`, then this function will
195-
throw an `UnexpectedValueException` instead.
207+
Once ANY promise rejects, this will try to `cancel()` all remaining promises
208+
and throw an `Exception`. If the promise did not reject with an `Exception`,
209+
then this function will throw an `UnexpectedValueException` instead.
196210

197-
If no $timeout is given and either promise stays pending, then this will
198-
potentially wait/block forever until the last promise is settled.
211+
If no `$timeout` argument is given and ANY promises stay pending, then this
212+
will potentially wait/block forever until the promise is fulfilled.
199213

200-
If a $timeout is given and either promise is still pending once the timeout
201-
triggers, this will `cancel()` all pending promises and throw a `TimeoutException`.
202-
This implies that if you pass a really small (or negative) value, it will still
203-
start a timer and will thus trigger at the earliest possible time in the future.
214+
If a `$timeout` argument is given and ANY promises are still pending once
215+
the timeout triggers, this will `cancel()` all pending promises and throw a
216+
`TimeoutException`. This implies that if you pass a really small (or negative)
217+
value, it will still start a timer and will thus trigger at the earliest
218+
possible time in the future.
204219

205220
## Install
206221

src/functions.php

Lines changed: 99 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,65 @@
1212
use React\Promise\Timer\TimeoutException;
1313

1414
/**
15-
* wait/sleep for $time seconds
15+
* Wait/sleep for `$time` seconds.
1616
*
17-
* The $time value will be used as a timer for the loop so that it keeps running
18-
* until the timeout triggers.
19-
* This implies that if you pass a really small (or negative) value, it will still
20-
* start a timer and will thus trigger at the earliest possible time in the future.
17+
* ```php
18+
* Block\sleep(1.5, $loop);
19+
* ```
20+
*
21+
* This function will only return after the given `$time` has elapsed. In the
22+
* meantime, the event loop will run any other events attached to the same loop
23+
* until the timer fires. If there are no other events attached to this loop,
24+
* it will behave similar to the built-in [`sleep()`](https://www.php.net/manual/en/function.sleep.php).
25+
*
26+
* Internally, the `$time` argument will be used as a timer for the loop so that
27+
* it keeps running until this timer triggers. This implies that if you pass a
28+
* really small (or negative) value, it will still start a timer and will thus
29+
* trigger at the earliest possible time in the future.
2130
*
2231
* @param float $time
2332
* @param LoopInterface $loop
33+
* @return void
2434
*/
2535
function sleep($time, LoopInterface $loop)
2636
{
2737
await(Timer\resolve($time, $loop), $loop);
2838
}
2939

3040
/**
31-
* block waiting for the given $promise to resolve
41+
* Block waiting for the given `$promise` to be fulfilled.
42+
*
43+
* ```php
44+
* $result = Block\await($promise, $loop, $timeout);
45+
* ```
3246
*
33-
* Once the promise is resolved, this will return whatever the promise resolves to.
47+
* This function will only return after the given `$promise` has settled, i.e.
48+
* either fulfilled or rejected. In the meantime, the event loop will run any
49+
* events attached to the same loop until the promise settles.
3450
*
35-
* Once the promise is rejected, this will throw whatever the promise rejected with.
36-
* If the promise did not reject with an `Exception`, then this function will
37-
* throw an `UnexpectedValueException` instead.
51+
* Once the promise is fulfilled, this function will return whatever the promise
52+
* resolved to.
3853
*
39-
* If no $timeout is given and the promise stays pending, then this will
40-
* potentially wait/block forever until the promise is settled.
54+
* Once the promise is rejected, this will throw whatever the promise rejected
55+
* with. If the promise did not reject with an `Exception`, then this function
56+
* will throw an `UnexpectedValueException` instead.
4157
*
42-
* If a $timeout is given and the promise is still pending once the timeout
43-
* triggers, this will cancel() the promise and throw a `TimeoutException`.
58+
* ```php
59+
* try {
60+
* $result = Block\await($promise, $loop);
61+
* // promise successfully fulfilled with $value
62+
* echo 'Result: ' . $result;
63+
* } catch (Exception $exception) {
64+
* // promise rejected with $exception
65+
* echo 'ERROR: ' . $exception->getMessage();
66+
* }
67+
* ```
68+
*
69+
* If no `$timeout` argument is given and the promise stays pending, then this
70+
* will potentially wait/block forever until the promise is settled.
71+
*
72+
* If a `$timeout` argument is given and the promise is still pending once the
73+
* timeout triggers, this will `cancel()` the promise and throw a `TimeoutException`.
4474
* This implies that if you pass a really small (or negative) value, it will still
4575
* start a timer and will thus trigger at the earliest possible time in the future.
4676
*
@@ -100,20 +130,37 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
100130
}
101131

102132
/**
103-
* wait for ANY of the given promises to resolve
133+
* Wait for ANY of the given promises to be fulfilled.
104134
*
105-
* Once the first promise is resolved, this will try to cancel() all
106-
* remaining promises and return whatever the first promise resolves to.
135+
* ```php
136+
* $promises = array(
137+
* $promise1,
138+
* $promise2
139+
* );
107140
*
108-
* If ALL promises fail to resolve, this will fail and throw an Exception.
141+
* $firstResult = Block\awaitAny($promises, $loop, $timeout);
109142
*
110-
* If no $timeout is given and either promise stays pending, then this will
111-
* potentially wait/block forever until the first promise is settled.
143+
* echo 'First result: ' . $firstResult;
144+
* ```
112145
*
113-
* If a $timeout is given and either promise is still pending once the timeout
114-
* triggers, this will cancel() all pending promises and throw a `TimeoutException`.
115-
* This implies that if you pass a really small (or negative) value, it will still
116-
* start a timer and will thus trigger at the earliest possible time in the future.
146+
* This function will only return after ANY of the given `$promises` has been
147+
* fulfilled or will throw when ALL of them have been rejected. In the meantime,
148+
* the event loop will run any events attached to the same loop.
149+
*
150+
* Once ANY promise is fulfilled, this function will return whatever this
151+
* promise resolved to and will try to `cancel()` all remaining promises.
152+
*
153+
* Once ALL promises reject, this function will fail and throw an `UnderflowException`.
154+
* Likewise, this will throw if an empty array of `$promises` is passed.
155+
*
156+
* If no `$timeout` argument is given and ALL promises stay pending, then this
157+
* will potentially wait/block forever until the promise is fulfilled.
158+
*
159+
* If a `$timeout` argument is given and ANY promises are still pending once
160+
* the timeout triggers, this will `cancel()` all pending promises and throw a
161+
* `TimeoutException`. This implies that if you pass a really small (or negative)
162+
* value, it will still start a timer and will thus trigger at the earliest
163+
* possible time in the future.
117164
*
118165
* @param array $promises
119166
* @param LoopInterface $loop
@@ -161,24 +208,39 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
161208
}
162209

163210
/**
164-
* wait for ALL of the given promises to resolve
211+
* Wait for ALL of the given promises to be fulfilled.
212+
*
213+
* ```php
214+
* $promises = array(
215+
* $promise1,
216+
* $promise2
217+
* );
218+
*
219+
* $allResults = Block\awaitAll($promises, $loop, $timeout);
165220
*
166-
* Once the last promise resolves, this will return an array with whatever
221+
* echo 'First promise resolved with: ' . $allResults[0];
222+
* ```
223+
*
224+
* This function will only return after ALL of the given `$promises` have been
225+
* fulfilled or will throw when ANY of them have been rejected. In the meantime,
226+
* the event loop will run any events attached to the same loop.
227+
*
228+
* Once ALL promises are fulfilled, this will return an array with whatever
167229
* each promise resolves to. Array keys will be left intact, i.e. they can
168230
* be used to correlate the return array to the promises passed.
169231
*
170-
* If ANY promise fails to resolve, this will try to cancel() all
171-
* remaining promises and throw an Exception.
172-
* If the promise did not reject with an `Exception`, then this function will
173-
* throw an `UnexpectedValueException` instead.
232+
* Once ANY promise rejects, this will try to `cancel()` all remaining promises
233+
* and throw an `Exception`. If the promise did not reject with an `Exception`,
234+
* then this function will throw an `UnexpectedValueException` instead.
174235
*
175-
* If no $timeout is given and either promise stays pending, then this will
176-
* potentially wait/block forever until the last promise is settled.
236+
* If no `$timeout` argument is given and ANY promises stay pending, then this
237+
* will potentially wait/block forever until the promise is fulfilled.
177238
*
178-
* If a $timeout is given and either promise is still pending once the timeout
179-
* triggers, this will cancel() all pending promises and throw a `TimeoutException`.< 9DAB /div>
180-
* This implies that if you pass a really small (or negative) value, it will still
181-
* start a timer and will thus trigger at the earliest possible time in the future.
239+
* If a `$timeout` argument is given and ANY promises are still pending once
240+
* the timeout triggers, this will `cancel()` all pending promises and throw a
241+
* `TimeoutException`. This implies that if you pass a really small (or negative)
242+
* value, it will still start a timer and will thus trigger at the earliest
243+
* possible time in the future.
182244
*
183245
* @param array $promises
184246
* @param LoopInterface $loop
@@ -210,6 +272,7 @@ function awaitAll(array $promises, LoopInterface $loop, $timeout = null)
210272
*
211273
* @internal
212274
* @param array $promises
275+
* @return void
213276
*/
214277
function _cancelAllPromises(array $promises)
215278
{

0 commit comments

Comments
 (0)
0