Skip to content

Commit f5e9505

Browse files
committed
Improve documentation and examples
1 parent bc01ce1 commit f5e9505

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ built on top of [ReactPHP](https://reactphp.org/).
1010
Let's say you crawl a page and find that you need to send 100 HTTP requests to
1111
following pages which each takes `0.2s`. You can either send them all
1212
sequentially (taking around `20s`) or you can use
13-
[ReactPHP](https://reactphp.org) to concurrently request all your pages at the
13+
[ReactPHP](https://reactphp.org/) to concurrently request all your pages at the
1414
same time. This works perfectly fine for a small number of operations, but
1515
sending an excessive number of requests can either take up all resources on your
1616
side or may get you banned by the remote side as it sees an unreasonable number
@@ -84,12 +84,14 @@ $q = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
8484
foreach ($urls as $url) {
8585
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) use ($url) {
8686
echo $url . ': ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
87-
});
87+
}, function (Exception $e) {
88+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
89+
} );
8890
}
8991

9092
```
9193

92-
See also the [examples](examples).
94+
See also the [examples](examples/).
9395

9496
## Usage
9597

@@ -196,6 +198,10 @@ interface that makes it easy to react to when an operation is completed (i.e.
196198
either successfully fulfilled or rejected with an error):
197199

198200
```php
201+
<?php
202+
203+
require __DIR__ . '/vendor/autoload.php';
204+
199205
$promise->then(
200206
function ($result) {
201207
var_dump('Result received', $result);
@@ -284,6 +290,10 @@ schedule all jobs while limiting concurrency to ensure no more than
284290
resolves with the results of all jobs on success.
285291

286292
```php
293+
<?php
294+
295+
require __DIR__ . '/vendor/autoload.php';
296+
287297
$browser = new React\Http\Browser();
288298

289299
$promise = Queue::all(3, $urls, function ($url) use ($browser) {
@@ -292,6 +302,8 @@ $promise = Queue::all(3, $urls, function ($url) use ($browser) {
292302

293303
$promise->then(function (array $responses) {
294304
echo 'All ' . count($responses) . ' successful!' . PHP_EOL;
305+
}, function (Exception $e) {
306+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
295307
});
296308
```
297309

@@ -360,6 +372,10 @@ resolves with the result of the first job on success and will then try
360372
to `cancel()` all outstanding jobs.
361373

362374
```php
375+
<?php
376+
377+
require __DIR__ . '/vendor/autoload.php';
378+
363379
$browser = new React\Http\Browser();
364380

365381
$promise = Queue::any(3, $urls, function ($url) use ($browser) {
@@ -368,6 +384,8 @@ $promise = Queue::any(3, $urls, function ($url) use ($browser) {
368384

369385
$promise->then(function (ResponseInterface $response) {
370386
echo 'First response: ' . $response->getBody() . PHP_EOL;
387+
}, function (Exception $e) {
388+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
371389
});
372390
```
373391

@@ -449,6 +467,10 @@ Similarly, you can also wrap this in a function to provide a simple API and hide
449467
all the async details from the outside:
450468

451469
```php
470+
<?php
471+
472+
require __DIR__ . '/vendor/autoload.php';
473+
452474
use function React\Async\await;
453475

454476
/**

examples/01-http.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ function (Exception $e) use ($url) {
3030
}
3131
);
3232
}
33-

examples/02-http-all.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ function ($e) {
3232
echo 'An error occurred: ' . $e->getMessage() . PHP_EOL;
3333
}
3434
);
35-

examples/03-http-any.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
2323
// return only the URL for the first successful response
2424
return $url;
25-
}
26-
);
25+
}, function (Exception $e) {
26+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
27+
});
2728
});
2829

2930
$promise->then(
@@ -34,4 +35,3 @@ function ($e) {
3435
echo 'An error occurred: ' . $e->getMessage() . PHP_EOL;
3536
}
3637
);
37-

0 commit comments

Comments
 (0)