diff --git a/README.md b/README.md index bc4add1..decec93 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 @@ -196,6 +198,10 @@ interface that makes it easy to react to when an operation is completed (i.e. either successfully fulfilled or rejected with an error): ```php +then( function ($result) { var_dump('Result received', $result); @@ -284,6 +290,10 @@ schedule all jobs while limiting concurrency to ensure no more than resolves with the results of all jobs on success. ```php +then(function (array $responses) { echo 'All ' . count($responses) . ' successful!' . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -360,6 +372,10 @@ resolves with the result of the first job on success and will then try to `cancel()` all outstanding jobs. ```php +then(function (ResponseInterface $response) { echo 'First response: ' . $response->getBody() . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -449,6 +467,10 @@ Similarly, you can also wrap this in a function to provide a simple API and hide all the async details from the outside: ```php +getMessage() . PHP_EOL; } ); - diff --git a/examples/03-http-any.php b/examples/03-http-any.php index 8093774..2823e8a 100644 --- a/examples/03-http-any.php +++ b/examples/03-http-any.php @@ -22,8 +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; + }); }); $promise->then( @@ -34,4 +35,3 @@ function ($e) { echo 'An error occurred: ' . $e->getMessage() . PHP_EOL; } ); -