2017-11-08 14:01:26 +01:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Loop;
|
2020-04-04 15:49:10 +02:00
|
|
|
use Amp\Promise;
|
2017-11-08 14:01:26 +01:00
|
|
|
|
|
|
|
require_once __DIR__ . "/../../vendor/autoload.php";
|
|
|
|
|
|
|
|
/**
|
2020-04-04 15:49:10 +02:00
|
|
|
* @return Promise<string>
|
2017-11-08 14:01:26 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
function jobSuccess()
|
|
|
|
{
|
2017-11-08 14:01:26 +01:00
|
|
|
$deferred = new Deferred();
|
|
|
|
|
|
|
|
// We delay Promise resolve for 1 sec to simulate some async job
|
|
|
|
Loop::delay(1 * 1000, function () use ($deferred) {
|
|
|
|
$deferred->resolve("value");
|
|
|
|
});
|
|
|
|
|
|
|
|
return $deferred->promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 15:49:10 +02:00
|
|
|
* @return Promise<string>
|
2017-11-08 14:01:26 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
function jobFail()
|
|
|
|
{
|
2017-11-08 14:01:26 +01:00
|
|
|
$deferred = new Deferred();
|
|
|
|
|
|
|
|
// We delay Promise fail for 2 sec to simulate some async job
|
|
|
|
Loop::delay(2 * 1000, function () use ($deferred) {
|
|
|
|
$deferred->fail(new Exception("force fail"));
|
|
|
|
});
|
|
|
|
|
|
|
|
return $deferred->promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
Loop::run(function () {
|
|
|
|
try {
|
|
|
|
$asyncOperation1 = yield jobSuccess();
|
|
|
|
echo "asyncOperation1 result -> " . $asyncOperation1 . PHP_EOL;
|
|
|
|
|
|
|
|
// jobFail() will start only after jobSuccess() is finished, all this will be executed asynchronous
|
|
|
|
$asyncOperation2 = yield jobFail();
|
|
|
|
echo "asyncOperation2 result -> " . $asyncOperation2 . PHP_EOL; //this statment will not run
|
|
|
|
} catch (Throwable $exception) {
|
|
|
|
echo "asyncOperation catch -> " . $exception->getMessage() . PHP_EOL;
|
|
|
|
}
|
|
|
|
});
|