Luminova Framework

PHP Luminova: Promise Utility Classes

Last updated: 2025-11-04 18:42:02

Use Luminova’s Deferred, Fulfilled, and Rejected classes to simplify promise handling, manage pre-resolved states, and improve async logic clarity.

Luminova provides a set of utility classes to simplify common promise patterns.These include Deferred, Fulfilled, and Rejected, each designed to handle different promise states or workflows efficiently.


Use Cases

ClassPurposeTypical Use Case
DeferredCreate and control promise resolution manuallyAsync callbacks, timeouts, or custom async logic
FulfilledRepresent an already completed promiseReturn cached or static data as a promise
RejectedRepresent an already failed promiseReturn a known failure immediately

Deferred

The Deferred class provides full control over a Promise object.It separates the creation of a promise from its resolution or rejection — ideal when the result of an operation isn’t immediately available, such as waiting for asynchronous tasks, I/O operations, or timed callbacks.

  • Class Namespace: Luminova\Utility\Promise\Deferred

Constructor:

Create a new Deferred instance with a pending promise state.

public __construct()

Usage

use Luminova\Utility\Promise\Deferred;

$deferred = new Deferred();

// Access the promise object
$deferred->promise()
    ->then(function ($result) {
        echo "Done: $result";
    })
    ->catch(function ($error) {
        echo "Failed: " . $error->getMessage();
    });

// Later in your code...
$deferred->resolve('Operation successful');

// or handle failure
// $deferred->reject(new Exception('Something went wrong'));

Common Use Case

Deferred promises are commonly used in custom async wrappers:

use Luminova\Utility\Async;
use Luminova\Utility\Promise\Deferred;

function delayedMessage(): PromiseInterface {
    $deferred = new Deferred();
    Async::setTimeout(function () use ($deferred) {
        $deferred->resolve('Done after delay');
    }, 1000);
    return $deferred->promise();
}

delayedMessage()->then(fn($msg) => echo $msg);

Fulfilled

The Fulfilled class represents a promise that is already resolved with a known value.This is helpful when you want to return a precomputed result as a promise, without additional asynchronous logic.

Constructor:

Creates a promise that is already fulfilled (resolved).

public __construct(mixed $value)

Parameters

ParameterTypeDescription
$valuemixedThe value used to immediately resolve the promise.

Usage

use Luminova\Utility\Promise\Fulfilled;

$promise = new Fulfilled('Resolved Value');

$promise->then(function ($value) {
    echo "Resolved: " . $value; // Output: Resolved: Resolved Value
});

Rejected

The Rejected class represents a promise that is already rejected with a specific error or exception.It's useful for returning an immediate failure state from a function that normally returns a promise.

Constructor:

Creates a promise that is already rejected.

public __construct(\Throwable $reason)

Parameters

ParameterTypeDescription
$reasonThrowableThe reason for the rejection, typically an exception or error.

Usage

use Luminova\Utility\Promise\Rejected;

$promise = new Rejected(new \Exception('An error occurred'));

$promise->catch(function (\Throwable $error) {
    echo "Error: " . $error->getMessage(); // Output: Error: An error occurred
});