Luminova Framework

PHP Luminova: Create an Already Rejected Promise

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

Wrap a known exception in an already rejected promise so callers can use the standard promise API.

Use Rejected when a function returns promises and already has an error, such as a failed input check. The promise starts in the rejected state, and the matching handler runs when you attach it.


Usage

use Luminova\Promise\Rejected;

$promise = new Rejected(
    new RuntimeException('The record was not found.')
);

$result = $promise->catch(
    static fn (Throwable $reason): string => 'Use the default record.'
);

echo $result->wait();
// Use the default record.

Class Definition


Methods

__construct

Create an already rejected promise.

public function __construct(private Throwable $reason)

Parameters:

ParameterTypeDescription
$reasonThrowableThe exception to store.

then

Run the rejection 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?callableUnused by this rejected promise.
$onRejected?callableRejection callback. Defaults to null.

Return:

PromiseInterface - The resulting promise, or this promise if there is no matching callback.


catch

Run the rejection callback immediately.

public function catch(callable $onRejected): PromiseInterface

Parameters:

ParameterTypeDescription
$onRejectedcallableRejection callback. Receives the stored exception.

Return:

PromiseInterface - The promise produced by the callback.


finally

Run a callback with the stored exception. When the callback completes successfully, the chain retains the original rejected 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 exception immediately.

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

Parameters:

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

Return:

Throwable - The stored rejection reason.


value

Read the stored exception.

public function value(): mixed

Return:

Throwable - The stored rejection reason.


state

Read the promise state.

public function state(): string

Return:

string - Promise::REJECTED.


getState

Read the same state as state().

public function getState(): string

Return:

string - Promise::REJECTED.


is

Compare the stored state to the requested state.

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

Parameters:

ParameterTypeDescription
$statestringState to check. Defaults to Promise::REJECTED.

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

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

public function resolve(mixed $value): void

Parameters:

ParameterTypeDescription
$valuemixedFulfillment value.

reject

A second rejection with the same exception has no effect. A different reason 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.