PHP code example of carno-php / promise
1. Go to this page and download the library: Download carno-php/promise 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/ */
carno-php / promise example snippets
$promise = new Promise(static function (Promised $promise) {
if (1) {
$promise->resolve('success');
} else {
$promise->reject('error');
}
});
$promise->then(function () {
echo 'promise has been resolved', PHP_EOL;
}, function () {
echo 'promise has been rejected', PHP_EOL;
});
$promise = Promise::deferred();
$promise->then(function (string $var) {
echo $var, PHP_EOL;
});
$promise->resolve('var');
$promise = Promise::resolved('var');
$promise->then(function (string $var) {
echo $var, PHP_EOL;
});
$promise = Promise::rejected(new Exception('Test'));
$promise->catch(function (Throwable $e) {
echo 'failure with ', $e->getMessage(), PHP_EOL;
});
$p1 = Promise::deferred();
$p2 = Promise::deferred();
$pa = Promise::all($p1, $p2);
$pa->then(function (array $results) {
echo 'all resovled with results ', var_export($results, true), PHP_EOL;
});
$p1->resolve('var1');
$p2->resolve('var2');
$p1 = Promise::deferred();
$p1->then(function () {
echo 'p1 has been resolved', PHP_EOL;
}, function () {
echo 'p1 has been rejected', PHP_EOL;
});
$p2 = Promise::deferred();
$p2->then(function () {
echo 'p2 has been resolved', PHP_EOL;
}, function () {
echo 'p2 has been rejected', PHP_EOL;
});
$pr = Promise::race($p1, $p2);
$pr->then(function (string $var) {
echo 'race result is ', $var, PHP_EOL;
});
$p1->resolve('test');
// or $p2->reject();
$promise = Promise::deferred();
echo '#1 promise is pended ? ', $promise->pended() ? 'yes' : 'no', PHP_EOL;
$promise->resolve();
echo '#2 promise is pended ? ', $promise->pended() ? 'yes' : 'no', PHP_EOL;
$promise = Promise::deferred();
echo '#1 promise is chained ? ', $promise->chained() ? 'yes' : 'no', PHP_EOL;
$promise->then(function () {
});
echo '#2 promise is chained ? ', $promise->chained() ? 'yes' : 'no', PHP_EOL;
$next = Promise::deferred();
$next->then(function (string $var) {
echo 'NEXT promise been resolved with ', $var, PHP_EOL;
}, function (string $var) {
echo 'NEXT promise been rejected with ', $var, PHP_EOL;
});
$promise = Promise::deferred()->sync($next);
$promise->then(function (string $var) {
echo 'CURRENT promise been resolved with ', $var, PHP_EOL;
}, function (string $var) {
echo 'CURRENT promise been rejected with ', $var, PHP_EOL;
});
$promise->resolve('hello');
// or $promise->reject('world');
Promise::deferred()->fusion()->throw(new Exception('test'));
$promise = Promise::deferred();
$promise->then(function (...$args) {
echo 'promise resolved with args ', var_export($args, true), PHP_EOL;
}, function (...$args) {
echo 'promise rejected with args ', var_export($args, true), PHP_EOL;
});
$promise->resolve('hello', 'world');
// or $promise->reject('hello', 'world');
Promise::deferred()->resolve('var1', 'var2');
Promise::deferred()->reject('var1', 'var2');
bash
composer
bash
php benchmark.php