Skip to content

Commit 42e6a0f

Browse files
authored
Merge pull request #97 from clue-labs/docs
Improve documentation for event semantics
2 parents caf646e + c57e850 commit 42e6a0f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ Interesting events emitted by Request:
4444
* `drain`: The outgoing buffer drained and the response is ready to accept more
4545
data for the next `write()` call.
4646
* `error`: An error occurred, an `Exception` is passed as first argument.
47+
If the response emits an `error` event, this will also be emitted here.
4748
* `close`: The request is closed. If an error occurred, this event will be
4849
preceeded by an `error` event.
50+
For a successful response, this will be emitted only once the response emits
51+
the `close` event.
4952

5053
Response implements ReadableStreamInterface.
5154
Interesting events emitted by Response:
@@ -54,9 +57,11 @@ Interesting events emitted by Response:
5457
When a response encounters a chunked encoded response it will parse it
5558
transparently for the user and removing the `Transfer-Encoding` header.
5659
* `error`: An error occurred, an `Exception` is passed as first argument.
60+
This will also be forwarded to the request and emit an `error` event there.
5761
* `end`: The response has been fully received.
5862
* `close`: The response is closed. If an error occured, this event will be
5963
preceeded by an `error` event.
64+
This will also be forwarded to the request and emit a `close` event there.
6065

6166
### Example
6267

@@ -68,9 +73,15 @@ $client = new React\HttpClient\Client($loop);
6873

6974
$request = $client->request('GET', 'https://github.com/');
7075
$request->on('response', function ($response) {
71-
$response->on('data', function ($data, $response) {
72-
// ...
76+
$response->on('data', function ($chunk) {
77+
echo $chunk;
7378
});
79+
$response->on('end', function() {
80+
echo 'DONE';
81+
});
82+
});
83+
$request->on('error', function (\Exception $e) {
84+
echo $e;
7485
});
7586
$request->end();
7687
$loop->run();

examples/01-google.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$loop = React\EventLoop\Factory::create();
99
$client = new Client($loop);
1010

11-
$request = $client->request('GET', 'https://google.com/');
11+
$request = $client->request('GET', isset($argv[1]) ? $argv[1] : 'https://google.com/');
1212

1313
$request->on('response', function (Response $response) {
1414
var_dump($response->getHeaders());
@@ -22,6 +22,10 @@
2222
});
2323
});
2424

25+
$request->on('error', function (\Exception $e) {
26+
echo $e;
27+
});
28+
2529
$request->end();
2630

2731
$loop->run();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy