PHP code example of clue / block-react

1. Go to this page and download the library: Download clue/block-react library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clue / block-react example snippets


// old
$result = Clue\React\Block\await($promise);

// new
$result = React\Async\await($promise);

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

    // set up two parallel requests
    $request1 = $browser->get('http://www.google.com/');
    $request2 = $browser->get('http://www.google.co.uk/');

    // keep the loop running (i.e. block) until the first response arrives
    $fasterResponse = Clue\React\Block\awaitAny(array($request1, $request2));

    return $fasterResponse->getBody();
}

Clue\React\Block\await(…);

use function Clue\React\Block\await;

await(…);

use Clue\React\Block;

Block\await(…);

Clue\React\Block\sleep(1.5, $loop);

$result = Clue\React\Block\await($promise);

try {
    $result = Clue\React\Block\await($promise);
    // promise successfully fulfilled with $result
    echo 'Result: ' . $result;
} catch (Exception $exception) {
    // promise rejected with $exception
    echo 'ERROR: ' . $exception->getMessage();
}

$promises = array(
    $promise1,
    $promise2
);

$firstResult = Clue\React\Block\awaitAny($promises);

echo 'First result: ' . $firstResult;

$promises = array(
    $promise1,
    $promise2
);

$allResults = Clue\React\Block\awaitAll($promises);

echo 'First promise resolved with: ' . $allResults[0];