PHP Luminova: Create an Already Fulfilled Promise
Wrap a known value in an already fulfilled promise so callers can use the standard promise API.
Use Fulfilled when a function returns promises and already has a result, such as a cached value. The promise starts in the fulfilled state, and the matching handler runs when you attach it.
Usage
use Luminova\Promise\Fulfilled;
$promise = new Fulfilled(42);
echo $promise->then(
static fn (int $value): int => $value * 2
)->wait();
// 84Class Definition
- Full namespace:
Luminova\Promise\Fulfilled - This class implements Luminova\Interface\PromiseInterface.
- This class is final.
Methods
__construct
Create an already fulfilled promise. Pass the value itself. Passing another PromiseInterface raises a RuntimeException.
public function __construct(private mixed $value)Parameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The value to store. |
then
Run the fulfillment callback immediately if supplied. Its result becomes the next promise. A thrown exception becomes a rejected promise.
public function then(?callable $onResolved = null, ?callable $onRejected = null): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onResolved | ?callable | Fulfillment callback. Defaults to null. |
$onRejected | ?callable | Unused by this fulfilled promise. |
Return:
PromiseInterface - The resulting promise, or this promise if there is no matching callback.
catch
Return the fulfilled promise directly.
public function catch(callable $onRejected): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onRejected | callable | Rejection callback. Unused for fulfilled promises. |
Return:
PromiseInterface - The current promise.
finally
Run a callback with the stored value. When the callback completes successfully, the chain retains the original fulfilled outcome.
public function finally(callable $onFinally): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onFinally | callable | Callback that receives the settled value or reason. |
Return:
PromiseInterface - The resulting promise.
wait
Read the stored value immediately.
public function wait(int $timeout = 1000): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$timeout | int | Accepted for interface compatibility. This promise is already settled. |
Return:
mixed - The stored fulfillment value.
value
Read the stored value.
public function value(): mixedReturn:
mixed - The stored fulfillment value.
state
Read the promise state.
public function state(): stringReturn:
string - Promise::FULFILLED.
getState
Read the same state as state().
public function getState(): stringReturn:
string - Promise::FULFILLED.
is
Compare the stored state to the requested state.
public function is(string $state = Promise::FULFILLED): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$state | string | State to check. Defaults to Promise::FULFILLED. |
Return:
bool - Whether the state matches.
error
Register a callback for invalid operations on this promise.
public function error(callable $onError): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$onError | callable | Callback that receives a promise operation error. |
Return:
self - The current promise.
resolve
A second resolution with the same value has no effect. A different value reports a promise operation error.
public function resolve(mixed $value): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | Fulfillment value. |
reject
Attempting to reject this settled promise reports a promise operation error.
public function reject(mixed $reason): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$reason | mixed | Reason for the rejection. |
canceled
Return this settled promise directly.
public function canceled(callable $onCancelled): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onCancelled | callable | Unused by this settled promise. |
Return:
PromiseInterface - The current promise.
cancel
A settled promise remains in its current state when canceled.
public function cancel(mixed $reason = null): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$reason | mixed | Optional cancellation reason. Unused by this settled promise. |