\Co\promise
###API
namespace Co;
function promise(Closure $closure): Promise;
Parameter descriptionβ
Parameters | Type | Description |
---|---|---|
$closure | Closure | Entry closure function, run immediately in the current context |
Closure parametersβ
Parameters | Type | Description |
---|---|---|
$resolve | Closure | Resolve contract |
$reject | Closure | Reject future contract |
Return valueβ
Returns a Promise
object
Overviewβ
Promise (promise/expiration mechanism) is a core concept in the ripple framework and is used to handle asynchronous operations. A Promise object represents the final completion or failure of an asynchronous operation, as well as its result value. Compared with Future, Promise provides a more intuitive API that is easier to understand and use.
Usageβ
Use this method to create a Promise object
$promise = \Co\promise(Closure $callback): Promise
ripple will provide two parameters for the closure function you submit, one is the
resolve
callback function and the other is thereject
callback function. Use these two callback functions to resolve or reject a futures contract.
$promise = \Co\promise(function ($resolve, $reject) {
$resolve('success'); // Resolve a future contract
// $reject('error'); // Reject a futures contract
//TODO: More actions...
});
Promise statusβ
A Promise object has three states:
- pending: initial state, neither success nor failure state. -fulfilled: means the operation was completed successfully.
- rejected: means the operation failed.
A Promise object can only be converted from the
pending
state to thefulfilled
orrejected
state. Once converted tofulfilled
orrejected
state, it cannot be converted to other states again.
$promise = \Co\promise(function ($resolve, $reject) {
//TODO: Asynchronous operations such as reading files/network requests/database queries...
$resolve('success'); // Resolve a future contract
});
// When the future contract is resolved, the callback function in the then method will be triggered.
$promise->then(function (mixed $result) {
echo $value;
});
// When the futures contract is rejected, the callback function in the catch method will be triggered.
$promise->catch(function (Exception $e) {
echo $reason;
});
$promise->await(); // Wait for the promise to be resolved
Notesβ
resolve
and reject
will only change the state and result of Promise and will not prevent subsequent code execution.