Luminova Framework

PHP Luminova: Create an Already Fulfilled Promise

Last updated: 2026-11-28 20:57:12

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();
// 84

Class Definition


Methods

__construct

Create an already fulfilled promise. Pass the value itself. Passing another PromiseInterface raises a RuntimeException.

public function __construct(private mixed $value)

Parameters:

ParameterTypeDescription
$valuemixedThe 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): PromiseInterface

Parameters:

ParameterTypeDescription
$onResolved?callableFulfillment callback. Defaults to null.
$onRejected?callableUnused 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): PromiseInterface

Parameters:

ParameterTypeDescription
$onRejectedcallableRejection 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): PromiseInterface

Parameters:

ParameterTypeDescription
$onFinallycallableCallback that receives the settled value or reason.

Return:

PromiseInterface - The resulting promise.


wait

Read the stored value immediately.

public function wait(int $timeout = 1000): mixed

Parameters:

ParameterTypeDescription
$timeoutintAccepted for interface compatibility. This promise is already settled.

Return:

mixed - The stored fulfillment value.


value

Read the stored value.

public function value(): mixed

Return:

mixed - The stored fulfillment value.


state

Read the promise state.

public function state(): string

Return:

string - Promise::FULFILLED.


getState

Read the same state as state().

public function getState(): string

Return:

string - Promise::FULFILLED.


is

Compare the stored state to the requested state.

public function is(string $state = Promise::FULFILLED): bool

Parameters:

ParameterTypeDescription
$statestringState 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): self

Parameters:

ParameterTypeDescription
$onErrorcallableCallback 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): void

Parameters:

ParameterTypeDescription
$valuemixedFulfillment value.

reject

Attempting to reject this settled promise reports a promise operation error.

public function reject(mixed $reason): void

Parameters:

ParameterTypeDescription
$reasonmixedReason for the rejection.

canceled

Return this settled promise directly.

public function canceled(callable $onCancelled): PromiseInterface

Parameters:

ParameterTypeDescription
$onCancelledcallableUnused 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): void

Parameters:

ParameterTypeDescription
$reasonmixedOptional cancellation reason. Unused by this settled promise.