Download the PHP package streamcommon/promise without Composer
On this page you can find all versions of the php package streamcommon/promise. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download streamcommon/promise
More information about streamcommon/promise
Files in streamcommon/promise
Package promise
Short Description PHP-CLI promise implementation
License BSD-3-Clause
Informations about the package promise
PHP Promises/A+ implementation
This package provides Promise/A+ PHP implementation.
Branches
Installation
Console run:
Or add into your composer.json
:
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.
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
$reject
to indicate that an error occurred and setPromise
state toPromiseInterface::STATE_REJECTED
Method PromiseInterface::then()
it be called after promise change stage. In terms of our analogy: this is the “subscription".
$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.
It is similar to:
Similarly PromiseInterface::reject()
creates an already executed promise with an error value.
It is similar to:
Sub promise
When function($resolve, $reject)
executor finishes the job, it can return PromiseInterface
.
In this case, it will wait for the execution of sub promise.
Method PromiseInterface::then()
can return PromiseInterface
to.
For more info check example scripts.
Example
Standard 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
Sub promise
Sub async promise
If use
ExtSwoolePromise
withdaemon|cycle|loop
you must useSwoole\Runtime::wait()