streamcommon / promise
PHP-CLI promise implementation
Installs: 2 029
Dependents: 0
Suggesters: 0
Security: 0
Stars: 15
Watchers: 2
Forks: 3
Open Issues: 0
Requires
- php: ^7.3
- doctrine/collections: ^1.6.4
Requires (Dev)
- ext-pcov: ^1.0.0
- ext-swoole: ^4.2.13
- brainmaestro/composer-git-hooks: ^2.8.3
- malukenho/docheader: ^0.1.8
- ocramius/package-versions: ^1.5.1
- php-coveralls/php-coveralls: ^2.2.0
- phpstan/phpstan: ^0.12.19
- phpunit/phpunit: ^9.1.3
- streamcommon/coding-standard: dev-master
Suggests
- ext-swoole: Install extension(^4.2.13) for using ExtSwoolePromise
This package is auto-updated.
Last update: 2024-12-29 02:13:42 UTC
README
This package provides Promise/A+ PHP implementation.
Branches
Installation
Console run:
composer require streamcommon/promise
Or add into your composer.json
:
"require": { "streamcommon/promise": "*" }
If you want see TRUE promise then install Swoole extension. For more info visit the Swoole repo
NOTE: TRUE promise work only in CLI mode
Promise
Promise is a library which provides Promise/A+ PHP implementation.
All Promise it a special PHP classes that contains its state:
pending
- PromiseInterface::STATE_PENDINGfulfilled
- PromiseInterface::STATE_FULFILLEDrejected
- PromiseInterface::STATE_REJECTED
To initiate a new promise, you can use static method PromiseInterface::create
or create with new.
All resulting Promise
has PromiseInterface::STATE_PENDING
state.
$promise = new Promise(function(callable $resolve, callable $reject)); // OR $promise = Promise::create(function(callable $resolve, callable $reject))
When function($resolve, $reject)
executor finishes the job, it should call one of the functions:
$resolve
to indicate that the job finished successfully and setPromise
state toPromiseInterface::STATE_FULFILLED
$resolve = function ($value) { $this->setState(PromiseInterface::STATE_FULFILLED); $this->setResult($value); };
$reject
to indicate that an error occurred and setPromise
state toPromiseInterface::STATE_REJECTED
$reject = function ($value) { $this->setState(PromiseInterface::STATE_REJECTED); $this->setResult($value); };
Method PromiseInterface::then()
it be called after promise change stage. In terms of our analogy: this is the “subscription".
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;
$onFulfilled
run when thePromise
is resolved and it hasPromiseInterface::STATE_FULFILLED
state.$onFulfilled
run when thePromise
is rejected and it hasPromiseInterface::STATE_REJECTED
state.
NOTE: If
$onFulfilled
or$onFulfilled
is not a callable function it was ignore
Calling PromiseInterface::resolve()
creates a successfully executed promise with the result value.
public static function resolve($value): PromiseInterface;
It is similar to:
$promise = new Promise(function(callable $resolve) { $resolve($value) });
Similarly PromiseInterface::reject()
creates an already executed promise with an error value.
public static function reject($value): PromiseInterface;
It is similar to:
$promise = new Promise(function(callable $resolve, callable $reject) { $reject($value) });
Sub promise
When function($resolve, $reject)
executor finishes the job, it can return PromiseInterface
.
$promise = Promise::create(function (callable $resolve) { $resolve(Promise::create(function (callable $subResolve) { $subResolve(42); })); });
In this case, it will wait for the execution of sub promise.
Method PromiseInterface::then()
can return PromiseInterface
to.
$promise->then(function ($value) { return Promise::create(function (callable $resolve) use ($value) { $resolve($value + 1); }); });
For more info check example scripts.
Example
Standard Promise
use Streamcommon\Promise\Promise; $promise = Promise::create(function (callable $resolve) { $resolve(41); }); $newPromise = $promise->then(function ($value) { return $value + 1; }); $promise->then(function ($value) { echo $value . ' === 41' . PHP_EOL; }); $newPromise->then(function ($value) { echo $value . ' === 42' . PHP_EOL; }); $promise->wait(); // promise execution
If you want see TRUE promise then install Swoole extension. For more info visit the Swoole repo
NOTE: TRUE promise work only in CLI mode
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; });
Sub promise
use Streamcommon\Promise\Promise; $promise = Promise::create(function (callable $resolve) { $resolve(Promise::create(function (callable $resolve) { $resolve(42); })); }); $newPromise = $promise->then(function ($value) { return $value + 1; }); $superNewPromise = $promise->then(function ($value) { return Promise::create(function (callable $resolve) use ($value) { $resolve($value + 2); }); }); $promise->then(function ($value) { echo $value . ' === 42' . PHP_EOL; }); $newPromise->then(function ($value) { echo $value . ' === 43' . PHP_EOL; }); $superNewPromise->then(function ($value) { echo $value . ' === 44' . PHP_EOL; }); $promise->wait();
Sub async promise
use Streamcommon\Promise\ExtSwoolePromise; // be careful with this \Swoole\Runtime::enableCoroutine(); // IF YOU WANT REALY ASYNC $promise = ExtSwoolePromise::create(function (callable $resolve) { $promise = ExtSwoolePromise::create(function (callable $resolve) { $resolve(41); }); $promise->then(function ($value) use ($resolve) { $resolve($value); }); }); $promise->then(function ($value) { return $value + 1; })->then(function ($value) { echo $value . PHP_EOL; });
If use
ExtSwoolePromise
withdaemon|cycle|loop
you must useSwoole\Runtime::wait()
\Swoole\Runtime::enableCoroutine(); while (true) { /// Some code with ExtSwoolePromise /// \Swoole\Runtime::wait(); }