Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ into your traditional, synchronous (blocking) application stack.
### Quickstart example

The following example code demonstrates how this library can be used along with
an [async HTTP client](https://github.com/clue/reactphp-buzz) to process two
an [async HTTP client](https://github.com/reactphp/http#client-usage) to process two
non-blocking HTTP requests and block until the first (faster) one resolves.

```php
Expand All @@ -44,7 +44,7 @@ function blockingExample()

// this example uses an HTTP client
// this could be pretty much everything that binds to an event loop
$browser = new Clue\React\Buzz\Browser($loop);
$browser = new React\Http\Browser($loop);

// set up two parallel requests
$request1 = $browser->get('http://www.google.com/');
Expand Down Expand Up @@ -128,14 +128,16 @@ will throw an `UnexpectedValueException` instead.
```php
try {
$result = Block\await($promise, $loop);
// promise successfully fulfilled with $value
// promise successfully fulfilled with $result
echo 'Result: ' . $result;
} catch (Exception $exception) {
// promise rejected with $exception
echo 'ERROR: ' . $exception->getMessage();
}
```

See also the [examples](examples/).

If no `$timeout` argument is given and the promise stays pending, then this
will potentially wait/block forever until the promise is settled.

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

See also the [examples](examples/).

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

See also the [examples](examples/).

This function will only return after ALL of the given `$promises` have been
fulfilled or will throw when ANY of them have been rejected. In the meantime,
the event loop will run any events attached to the same loop.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"react/promise-timer": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/http": "^1.0"
}
}
36 changes: 36 additions & 0 deletions examples/01-await.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Clue\React\Block;

require __DIR__ . '/../vendor/autoload.php';

/**
* @param string $url
* @return Psr\Http\Message\ResponseInterface
* @throws Exception when HTTP request fails
*/
function requestHttp($url)
{
// use a unique event loop instance for this operation
$loop = React\EventLoop\Factory::create();

// This example uses an HTTP client
$browser = new React\Http\Browser($loop);

// set up one request
$promise = $browser->get($url);

try {
// keep the loop running (i.e. block) until the response arrives
$result = Block\await($promise, $loop);

// promise successfully fulfilled with $result
return $result;
} catch (Exception $exception) {
// promise rejected with $exception
throw $exception;
}
}

$response = requestHttp('http://www.google.com/');
echo $response->getBody();
40 changes: 40 additions & 0 deletions examples/02-await-any.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Clue\React\Block;

require __DIR__ . '/../vendor/autoload.php';

/**
* @param string $url1
* @param string $url2
* @return Psr\Http\Message\ResponseInterface
* @throws Exception when HTTP request fails
*/
function requestHttpFastestOfMultiple($url1, $url2)
{
// use a unique event loop instance for all parallel operations
$loop = React\EventLoop\Factory::create();

// This example uses an HTTP client
$browser = new React\Http\Browser($loop);

// set up two parallel requests
$promises = array(
$browser->get($url1),
$browser->get($url2)
);

try {
// keep the loop running (i.e. block) until the first response arrives
$fasterResponse = Block\awaitAny($promises, $loop);

// promise successfully fulfilled with $fasterResponse
return $fasterResponse;
} catch (Exception $exception) {
// promise rejected with $exception
throw $exception;
}
}

$first = requestHttpFastestOfMultiple('http://www.google.com/', 'http://www.google.co.uk/');
echo $first->getBody();
41 changes: 41 additions & 0 deletions examples/03-await-all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Clue\React\Block;

require __DIR__ . '/../vendor/autoload.php';

/**
* @param string $url1
* @param string $url2
* @return Psr\Http\Message\ResponseInterface[]
* @throws Exception when HTTP request fails
*/
function requestHttpMultiple($url1, $url2)
{
// use a unique event loop instance for all parallel operations
$loop = React\EventLoop\Factory::create();

// This example uses an HTTP client
$browser = new React\Http\Browser($loop);

// set up two parallel requests
$promises = array(
$browser->get($url1),
$browser->get($url2)
);

try {
// keep the loop running (i.e. block) until all responses arrive
$allResults = Block\awaitAll($promises, $loop);

// promise successfully fulfilled with $allResults
return $allResults;
} catch (Exception $exception) {
// promise rejected with $exception
throw $exception;
}
}

$all = requestHttpMultiple('http://www.google.com/', 'http://www.google.co.uk/');
echo 'First promise resolved with: ' . $all[0]->getBody() . PHP_EOL;
echo 'Second promise resolved with: ' . $all[1]->getBody() . PHP_EOL;
8 changes: 7 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ function sleep($time, LoopInterface $loop)
* ```php
* try {
* $result = Block\await($promise, $loop);
* // promise successfully fulfilled with $value
* // promise successfully fulfilled with $result
* echo 'Result: ' . $result;
* } catch (Exception $exception) {
* // promise rejected with $exception
* echo 'ERROR: ' . $exception->getMessage();
* }
* ```
*
* See also the [examples](../examples/).
*
* If no `$timeout` argument is given and the promise stays pending, then this
* will potentially wait/block forever until the promise is settled.
*
Expand Down Expand Up @@ -143,6 +145,8 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
* echo 'First result: ' . $firstResult;
* ```
*
* See also the [examples](../examples/).
*
* This function will only return after ANY of the given `$promises` has been
* fulfilled or will throw when ALL of them have been rejected. In the meantime,
* the event loop will run any events attached to the same loop.
Expand Down Expand Up @@ -221,6 +225,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
* echo 'First promise resolved with: ' . $allResults[0];
* ```
*
* See also the [examples](../examples/).
*
* This function will only return after ALL of the given `$promises` have been
* fulfilled or will throw when ANY of them have been rejected. In the meantime,
* the event loop will run any events attached to the same loop.
Expand Down