mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Add basic promise examples (#179)
This commit is contained in:
parent
19113ed5f1
commit
abe92a78ad
48
examples/promises/deferred.php
Normal file
48
examples/promises/deferred.php
Normal file
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
use Amp\Deferred;
|
||||
use Amp\Loop;
|
||||
|
||||
require_once __DIR__ . "/../../vendor/autoload.php";
|
||||
|
||||
/**
|
||||
* @return \Amp\Promise<string>
|
||||
*/
|
||||
function jobSuccess() {
|
||||
$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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Amp\Promise<string>
|
||||
*/
|
||||
function jobFail() {
|
||||
$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;
|
||||
}
|
||||
});
|
56
examples/promises/onresolve.php
Normal file
56
examples/promises/onresolve.php
Normal file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
use Amp\Deferred;
|
||||
use Amp\Loop;
|
||||
|
||||
require_once __DIR__ . "/../../vendor/autoload.php";
|
||||
|
||||
/**
|
||||
* @return \Amp\Promise<string>
|
||||
*/
|
||||
function jobSuccess() {
|
||||
$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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Amp\Promise<string>
|
||||
*/
|
||||
function jobFail() {
|
||||
$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();
|
||||
}
|
||||
|
||||
// onResolve() shouldn't be used directly in 99% of all cases.
|
||||
// Check https://github.com/amphp/amp/issues/178#issuecomment-342460585
|
||||
// Check deferred.php for a cleaner code syntax.
|
||||
Loop::run(function () {
|
||||
jobSuccess()->onResolve(function (Throwable $error = null, $result = null) {
|
||||
if ($error) {
|
||||
echo "asyncOperation1 fail -> " . $error->getMessage() . PHP_EOL;
|
||||
} else {
|
||||
echo "asyncOperation1 result -> " . $result . PHP_EOL;
|
||||
}
|
||||
|
||||
jobFail()->onResolve(function (Throwable $error = null, $result = null) {
|
||||
if ($error) {
|
||||
echo "asyncOperation2 fail -> " . $error->getMessage() . PHP_EOL;
|
||||
} else {
|
||||
echo "asyncOperation2 result -> " . $result . PHP_EOL;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user