From 0426973c183712c3a98f12d487a516e82cad0567 Mon Sep 17 00:00:00 2001 From: Yada Clintjens Date: Tue, 20 Feb 2024 15:39:14 +0100 Subject: [PATCH] Improve documentation and examples --- README.md | 10 ++++++++-- examples/01-http.php | 1 - examples/02-http-all.php | 1 - examples/03-http-any.php | 5 +++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bc4add1..7fb8933 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ built on top of [ReactPHP](https://reactphp.org/). Let's say you crawl a page and find that you need to send 100 HTTP requests to following pages which each takes `0.2s`. You can either send them all sequentially (taking around `20s`) or you can use -[ReactPHP](https://reactphp.org) to concurrently request all your pages at the +[ReactPHP](https://reactphp.org/) to concurrently request all your pages at the same time. This works perfectly fine for a small number of operations, but sending an excessive number of requests can either take up all resources on your 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) { foreach ($urls as $url) { $q($url)->then(function (Psr\Http\Message\ResponseInterface $response) use ($url) { echo $url . ': ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL; + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); } ``` -See also the [examples](examples). +See also the [examples](examples/). ## Usage @@ -292,6 +294,8 @@ $promise = Queue::all(3, $urls, function ($url) use ($browser) { $promise->then(function (array $responses) { echo 'All ' . count($responses) . ' successful!' . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -368,6 +372,8 @@ $promise = Queue::any(3, $urls, function ($url) use ($browser) { $promise->then(function (ResponseInterface $response) { echo 'First response: ' . $response->getBody() . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` diff --git a/examples/01-http.php b/examples/01-http.php index 0248fd7..608298c 100644 --- a/examples/01-http.php +++ b/examples/01-http.php @@ -30,4 +30,3 @@ function (Exception $e) use ($url) { } ); } - diff --git a/examples/02-http-all.php b/examples/02-http-all.php index 0cda97c..4dbb11e 100644 --- a/examples/02-http-all.php +++ b/examples/02-http-all.php @@ -32,4 +32,3 @@ function ($e) { echo 'An error occurred: ' . $e->getMessage() . PHP_EOL; } ); - diff --git a/examples/03-http-any.php b/examples/03-http-any.php index 8093774..33179a5 100644 --- a/examples/03-http-any.php +++ b/examples/03-http-any.php @@ -22,7 +22,9 @@ function (Psr\Http\Message\ResponseInterface $response) use ($url) { // return only the URL for the first successful response return $url; - } + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; + } ); }); @@ -34,4 +36,3 @@ function ($e) { echo 'An error occurred: ' . $e->getMessage() . PHP_EOL; } ); -