1. Go to this page and download the library: Download streamcommon/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/ */
streamcommon / promise example snippets
$promise = new Promise(function(callable $resolve, callable $reject));
// OR
$promise = Promise::create(function(callable $resolve, callable $reject))
$resolve = function ($value) {
$this->setState(PromiseInterface::STATE_FULFILLED);
$this->setResult($value);
};
$reject = function ($value) {
$this->setState(PromiseInterface::STATE_REJECTED);
$this->setResult($value);
};
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;
public static function resolve($value): PromiseInterface;
$promise = new Promise(function(callable $resolve) {
$resolve($value)
});
public static function reject($value): PromiseInterface;
$promise = new Promise(function(callable $resolve, callable $reject) {
$reject($value)
});
use Streamcommon\Promise\ExtSwoolePromise;
// be careful with this
\Swoole\Runtime::enableCoroutine(); // IF YOU WANT REALY ASYNC
$promise = ExtSwoolePromise::create(function (callable $resolve) {
// the function is executed automatically when the promise is constructed
$resolve(41);
});
$promise->then(function ($value) {
// the function is executed automatically after __constructor job
return $value + 1;
})->then(function ($value) {
// the function is executed automatically after ::then()
echo $value . PHP_EOL;
});