PHP Luminova: Create an Already Rejected Promise
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
- Full namespace:
Luminova\Promise\Rejected - This class implements Luminova\Interface\PromiseInterface.
- This class is final.
Methods
__construct
Create an already rejected promise.
public function __construct(private Throwable $reason)Parameters:
| Parameter | Type | Description |
|---|---|---|
$reason | Throwable | The 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): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onResolved | ?callable | Unused by this rejected promise. |
$onRejected | ?callable | Rejection 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): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onRejected | callable | Rejection 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): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$onFinally | callable | Callback that receives the settled value or reason. |
Return:
PromiseInterface - The resulting promise.
wait
Read the stored exception immediately.
public function wait(int $timeout = 1000): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$timeout | int | Accepted for interface compatibility. This promise is already settled. |
Return:
Throwable - The stored rejection reason.
value
Read the stored exception.
public function value(): mixedReturn:
Throwable - The stored rejection reason.
state
Read the promise state.
public function state(): stringReturn:
string - Promise::REJECTED.
getState
Read the same state as state().
public function getState(): stringReturn:
string - Promise::REJECTED.
is
Compare the stored state to the requested state.
public function is(string $state = Promise::REJECTED): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$state | string | State 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$onError | callable | Callback 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): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | Fulfillment 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): 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. |