@@ -7,17 +7,17 @@ A trivial implementation of timeouts for `Promise`s, built on top of [ReactPHP](
77** Table of contents**
88
99* [ Usage] ( #usage )
10- * [ timeout()] ( #timeout )
11- * [ Timeout cancellation] ( #timeout-cancellation )
12- * [ Cancellation handler] ( #cancellation-handler )
13- * [ Input cancellation] ( #input-cancellation )
14- * [ Output cancellation] ( #output-cancellation )
15- * [ Collections] ( #collections )
16- * [ resolve()] ( #resolve )
17- * [ Resolve cancellation] ( #resolve-cancellation )
18- * [ reject()] ( #reject )
19- * [ Reject cancellation] ( #reject-cancellation )
20- * [ TimeoutException] ( #timeoutexception )
10+ * [ timeout()] ( #timeout )
11+ * [ Timeout cancellation] ( #timeout-cancellation )
12+ * [ Cancellation handler] ( #cancellation-handler )
13+ * [ Input cancellation] ( #input-cancellation )
14+ * [ Output cancellation] ( #output-cancellation )
15+ * [ Collections] ( #collections )
16+ * [ resolve()] ( #resolve )
17+ * [ Resolve cancellation] ( #resolve-cancellation )
18+ * [ reject()] ( #reject )
19+ * [ Reject cancellation] ( #reject-cancellation )
20+ * [ TimeoutException] ( #timeoutexception )
2121* [ Install] ( #install )
2222* [ Tests] ( #tests )
2323* [ License] ( #license )
@@ -27,18 +27,26 @@ A trivial implementation of timeouts for `Promise`s, built on top of [ReactPHP](
2727This lightweight library consists only of a few simple functions.
2828All functions reside under the ` React\Promise\Timer ` namespace.
2929
30- The below examples assume you use an import statement similar to this:
30+ The below examples refer to all functions with their fully-qualified names like this:
3131
3232``` php
33- use React\Promise\Timer;
33+ React\Promise\Timer\timeout(…);
34+ ```
3435
35- Timer\timeout(…);
36+ As of PHP 5.6+ you can also import each required function into your code like this:
37+
38+ ``` php
39+ use function React\Promise\Timer\timeout;
40+
41+ timeout(…);
3642```
3743
38- Alternatively, you can also refer to them with their fully-qualified name :
44+ Alternatively, you can also use an import statement similar to this :
3945
4046``` php
41- \React\Promise\Timer\timeout(…);
47+ use React\Promise\Timer;
48+
49+ Timer\timeout(…);
4250```
4351
4452### timeout()
@@ -70,7 +78,7 @@ A common use case for handling only resolved values looks like this:
7078
7179``` php
7280$promise = accessSomeRemoteResource();
73- Timer\timeout($promise, 10.0)->then(function ($value) {
81+ React\Promise\ Timer\timeout($promise, 10.0)->then(function ($value) {
7482 // the operation finished within 10.0 seconds
7583});
7684```
@@ -79,12 +87,12 @@ A more complete example could look like this:
7987
8088``` php
8189$promise = accessSomeRemoteResource();
82- Timer\timeout($promise, 10.0)->then(
90+ React\Promise\ Timer\timeout($promise, 10.0)->then(
8391 function ($value) {
8492 // the operation finished within 10.0 seconds
8593 },
8694 function ($error) {
87- if ($error instanceof Timer\TimeoutException) {
95+ if ($error instanceof React\Promise\ Timer\TimeoutException) {
8896 // the operation has failed due to a timeout
8997 } else {
9098 // the input operation has failed due to some other error
@@ -96,11 +104,11 @@ Timer\timeout($promise, 10.0)->then(
96104Or if you're using [ react/promise v2.2.0] ( https://github.com/reactphp/promise ) or up:
97105
98106``` php
99- Timer\timeout($promise, 10.0)
107+ React\Promise\ Timer\timeout($promise, 10.0)
100108 ->then(function ($value) {
101109 // the operation finished within 10.0 seconds
102110 })
103- ->otherwise(function (Timer\TimeoutException $error) {
111+ ->otherwise(function (React\Promise\ Timer\TimeoutException $error) {
104112 // the operation has failed due to a timeout
105113 })
106114 ->otherwise(function ($error) {
@@ -178,7 +186,7 @@ input `$promise`, as demonstrated in the following example:
178186
179187``` php
180188$promise = accessSomeRemoteResource();
181- $timeout = Timer\timeout($promise, 10.0);
189+ $timeout = React\Promise\ Timer\timeout($promise, 10.0);
182190
183191$promise->cancel();
184192```
@@ -201,7 +209,7 @@ Similarily, you can also explicitly `cancel()` the resulting promise like this:
201209
202210``` php
203211$promise = accessSomeRemoteResource();
204- $timeout = Timer\timeout($promise, 10.0);
212+ $timeout = React\Promise\ Timer\timeout($promise, 10.0);
205213
206214$timeout->cancel();
207215```
@@ -237,7 +245,7 @@ This is done for consistency with the [timeout cancellation](#timeout-cancellati
237245handling and also because it is assumed this is often used like this:
238246
239247``` php
240- $timeout = Timer\timeout(accessSomeRemoteResource(), 10.0);
248+ $timeout = React\Promise\ Timer\timeout(accessSomeRemoteResource(), 10.0);
241249
242250$timeout->cancel();
243251```
@@ -264,7 +272,7 @@ $promises = array(
264272
265273$promise = \React\Promise\all($promises);
266274
267- Timer\timeout($promise, 10)->then(function ($values) {
275+ React\Promise\ Timer\timeout($promise, 10)->then(function ($values) {
268276 // *all* promises resolved
269277});
270278```
@@ -280,7 +288,7 @@ The `resolve($time, LoopInterface $loop = null)` function can be used to create
280288resolves in ` $time ` seconds with the ` $time ` as the fulfillment value.
281289
282290``` php
283- Timer\resolve(1.5)->then(function ($time) {
291+ React\Promise\ Timer\resolve(1.5)->then(function ($time) {
284292 echo 'Thanks for waiting ' . $time . ' seconds' . PHP_EOL;
285293});
286294```
@@ -301,7 +309,7 @@ loop instance.
301309You can explicitly ` cancel() ` the resulting timer promise at any time:
302310
303311``` php
304- $timer = Timer\resolve(2.0);
312+ $timer = React\Promise\ Timer\resolve(2.0);
305313
306314$timer->cancel();
307315```
@@ -314,7 +322,7 @@ The `reject($time, LoopInterface $loop = null)` function can be used to create a
314322which rejects in ` $time ` seconds with a ` TimeoutException ` .
315323
316324``` php
317- Timer\reject(2.0)->then(null, function (TimeoutException $e) {
325+ React\Promise\ Timer\reject(2.0)->then(null, function (React\Promise\Timer\ TimeoutException $e) {
318326 echo 'Rejected after ' . $e->getTimeout() . ' seconds ' . PHP_EOL;
319327});
320328```
@@ -338,7 +346,7 @@ and can be used as a basic building block for higher-level promise consumers.
338346You can explicitly ` cancel() ` the resulting timer promise at any time:
339347
340348``` php
341- $timer = Timer\reject(2.0);
349+ $timer = React\Promise\ Timer\reject(2.0);
342350
343351$timer->cancel();
344352```
@@ -353,7 +361,7 @@ The `getTimeout()` method can be used to get the timeout value in seconds.
353361
354362## Install
355363
356- The recommended way to install this library is [ through Composer] ( https://getcomposer.org ) .
364+ The recommended way to install this library is [ through Composer] ( https://getcomposer.org/ ) .
357365[ New to Composer?] ( https://getcomposer.org/doc/00-intro.md )
358366
359367This project follows [ SemVer] ( https://semver.org/ ) .
@@ -368,12 +376,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
368376This project aims to run on any platform and thus does not require any PHP
369377extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
370378HHVM.
371- It's * highly recommended to use PHP 7+ * for this project.
379+ It's * highly recommended to use the latest supported PHP version * for this project.
372380
373381## Tests
374382
375383To run the test suite, you first need to clone this repo and then install all
376- dependencies [ through Composer] ( https://getcomposer.org ) :
384+ dependencies [ through Composer] ( https://getcomposer.org/ ) :
377385
378386``` bash
379387$ composer install
@@ -382,7 +390,7 @@ $ composer install
382390To run the test suite, go to the project root and run:
383391
384392``` bash
385- $ php vendor/bin/phpunit
393+ $ vendor/bin/phpunit
386394```
387395
388396## License
0 commit comments