Skip to content

Commit d5c0384

Browse files
authored
Merge pull request #3 from clue-labs/timeout
Documentation for timeout handling
2 parents 6d1594d + 68348fd commit d5c0384

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ much any API that already uses Promises.
3737
* [Queue](#queue)
3838
* [Promises](#promises)
3939
* [Cancellation](#cancellation)
40+
* [Timeout](#timeout)
4041
* [Install](#install)
4142
* [Tests](#tests)
4243
* [License](#license)
@@ -215,6 +216,44 @@ $loop->addTimer(2.0, function () use ($promise) {
215216
Similarly, cancelling an operation that is queued and has not yet been started
216217
will be rejected without ever starting the operation.
217218

219+
#### Timeout
220+
221+
By default, this library does not limit how long a single operation can take,
222+
so that the resulting promise may stay pending for a long time.
223+
Many use cases involve some kind of "timeout" logic so that an operation is
224+
cancelled after a certain threshold is reached.
225+
226+
You can simply use [cancellation](#cancellation) as in the previous chapter or
227+
you may want to look into using [react/promise-timer](https://github.com/reactphp/promise-timer)
228+
which helps taking care of this through a simple API.
229+
230+
The resulting code with timeouts applied look something like this:
231+
232+
```php
233+
use React\Promise\Timer;
234+
235+
$q = new Queue(10, null, function ($uri) use ($browser, $loop) {
236+
return Timer\timeout($browser->get($uri), 2.0, $loop);
237+
});
238+
239+
$promise = $q($uri);
240+
```
241+
242+
The resulting promise can be consumed as usual and the above code will ensure
243+
that execution of this operation can not take longer than the given timeout
244+
(i.e. after it is actually started).
245+
In particular, note how this differs from applying a timeout to the resulting
246+
promise. The following code will ensure that the total time for queuing and
247+
executing this operation can not take longer than the given timeout:
248+
249+
```php
250+
// usually not recommended
251+
$promise = Timer\timeout($q($url), 2.0, $loop);
252+
```
253+
254+
Please refer to [react/promise-timer](https://github.com/reactphp/promise-timer)
255+
for more details.
256+
218257
## Install
219258

220259
The recommended way to install this library is [through Composer](https://getcomposer.org).

0 commit comments

Comments
 (0)