Skip to content

Commit aa438b2

Browse files
committed
Add examples
1 parent 07ee773 commit aa438b2

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ try {
136136
}
137137
```
138138

139+
See also the [examples](examples/).
140+
139141
If no `$timeout` argument is given and the promise stays pending, then this
140142
will potentially wait/block forever until the promise is settled.
141143

@@ -160,6 +162,8 @@ $firstResult = Block\awaitAny($promises, $loop, $timeout);
160162
echo 'First result: ' . $firstResult;
161163
```
162164

165+
See also the [examples](examples/).
166+
163167
This function will only return after ANY of the given `$promises` has been
164168
fulfilled or will throw when ALL of them have been rejected. In the meantime,
165169
the event loop will run any events attached to the same loop.
@@ -195,6 +199,8 @@ $allResults = Block\awaitAll($promises, $loop, $timeout);
195199
echo 'First promise resolved with: ' . $allResults[0];
196200
```
197201

202+
See also the [examples](examples/).
203+
198204
This function will only return after ALL of the given `$promises` have been
199205
fulfilled or will throw when ANY of them have been rejected. In the meantime,
200206
the event loop will run any events attached to the same loop.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react/promise-timer": "^1.5"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
26+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
27+
"react/http": "^1.0"
2728
}
2829
}

examples/01-await.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Clue\React\Block;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
/**
8+
* @param string $url
9+
* @return Psr\Http\Message\ResponseInterface
10+
* @throws Exception when HTTP request fails
11+
*/
12+
function requestHttp($url)
13+
{
14+
// use a unique event loop instance for this operation
15+
$loop = React\EventLoop\Factory::create();
16+
17+
// This example uses an HTTP client
18+
$browser = new React\Http\Browser($loop);
19+
20+
// set up one request
21+
$promise = $browser->get($url);
22+
23+
try {
24+
// keep the loop running (i.e. block) until the response arrives
25+
$result = Block\await($promise, $loop);
26+
27+
// promise successfully fulfilled with $result
28+
return $result;
29+
} catch (Exception $exception) {
30+
// promise rejected with $exception
31+
throw $exception;
32+
}
33+
}
34+
35+
$response = requestHttp('http://www.google.com/');
36+
echo $response->getBody();

examples/02-await-any.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Clue\React\Block;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
/**
8+
* @param string $url1
9+
* @param string $url2
10+
* @return Psr\Http\Message\ResponseInterface
11+
* @throws Exception when HTTP request fails
12+
*/
13+
function requestHttpFastestOfMultiple($url1, $url2)
14+
{
15+
// use a unique event loop instance for all parallel operations
16+
$loop = React\EventLoop\Factory::create();
17+
18+
// This example uses an HTTP client
19+
$browser = new React\Http\Browser($loop);
20+
21+
// set up two parallel requests
22+
$promises = array(
23+
$browser->get($url1),
24+
$browser->get($url2)
25+
);
26+
27+
try {
28+
// keep the loop running (i.e. block) until the first response arrives
29+
$fasterResponse = Block\awaitAny($promises, $loop);
30+
31+
// promise successfully fulfilled with $fasterResponse
32+
return $fasterResponse;
33+
} catch (Exception $exception) {
34+
// promise rejected with $exception
35+
throw $exception;
36+
}
37+
}
38+
39+
$first = requestHttpFastestOfMultiple('http://www.google.com/', 'http://www.google.co.uk/');
40+
echo $first->getBody();

examples/03-await-all.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Clue\React\Block;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
/**
8+
* @param string $url1
9+
* @param string $url2
10+
* @return Psr\Http\Message\ResponseInterface[]
11+
* @throws Exception when HTTP request fails
12+
*/
13+
function requestHttpMultiple($url1, $url2)
14+
{
15+
// use a unique event loop instance for all parallel operations
16+
$loop = React\EventLoop\Factory::create();
17+
18+
// This example uses an HTTP client
19+
$browser = new React\Http\Browser($loop);
20+
21+
// set up two parallel requests
22+
$promises = array(
23+
$browser->get($url1),
24+
$browser->get($url2)
25+
);
26+
27+
try {
28+
// keep the loop running (i.e. block) until all responses arrive
29+
$allResults = Block\awaitAll($promises, $loop);
30+
31+
// promise successfully fulfilled with $allResults
32+
return $allResults;
33+
} catch (Exception $exception) {
34+
// promise rejected with $exception
35+
throw $exception;
36+
}
37+
}
38+
39+
$all = requestHttpMultiple('http://www.google.com/', 'http://www.google.co.uk/');
40+
echo 'First promise resolved with: ' . $all[0]->getBody() . PHP_EOL;
41+
echo 'Second promise resolved with: ' . $all[1]->getBody() . PHP_EOL;

src/functions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ function sleep($time, LoopInterface $loop)
6666
* }
6767
* ```
6868
*
69+
* See also the [examples](../examples/).
70+
*
6971
* If no `$timeout` argument is given and the promise stays pending, then this
7072
* will potentially wait/block forever until the promise is settled.
7173
*
@@ -143,6 +145,8 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
143145
* echo 'First result: ' . $firstResult;
144146
* ```
145147
*
148+
* See also the [examples](../examples/).
149+
*
146150
* This function will only return after ANY of the given `$promises` has been
147151
* fulfilled or will throw when ALL of them have been rejected. In the meantime,
148152
* the event loop will run any events attached to the same loop.
@@ -221,6 +225,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
221225
* echo 'First promise resolved with: ' . $allResults[0];
222226
* ```
223227
*
228+
* See also the [examples](../examples/).
229+
*
224230
* This function will only return after ALL of the given `$promises` have been
225231
* fulfilled or will throw when ANY of them have been rejected. In the meantime,
226232
* the event loop will run any events attached to the same loop.

0 commit comments

Comments
 (0)