1. Go to this page and download the library: Download choval/async 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/ */
choval / async example snippets
function future($i=0)
{
return new React\Promise\FulfilledPromise($i+1);
}
$fn = function () {
throw new \Exception('hey!');
};
$promise = Async\silent($fn, $e);
// Promise resolves with null
// $e will hold an the hey! exception
Async\execute('echo "Wazza"')
->then(function ($output) {
// $output contains Wazza\n
})
->otherwise(function ($e) {
// Throws an Exception if the execution fails
// ie: 127 if the command does not exist
$exitCode = $e->getCode();
});
$times = 5;
$func = function () use (&$times) {
if(--$times) {
throw new \Exception('bad error');
}
return 'ok';
};
$retries = 6;
Async\retry($func, $retries, 0.1, 'bad error')
->then(function ($res) {
// $res is 'ok'
});
/**
* @param callable $func
* @param int $retries=10 (optional)
* @param float $frequency=0.001 (optional)
* @param string $ignoress (optional) The Throwable class to catch or string to match against Exception->getMessage()
*
* @return Promise
*/
$func = function () {
yield Async\sleep(2);
return true;
};
Async\wait(Async\timeout($func, 1.5));
// Throws an Exception due to the timeout 1.5 < 2