The promise utility wraps any native Promise, executor function, or ReactivePromise into a ComputedPromise β an object that is both awaitable like a regular promise and reactive like a signal.
Wraps a promise, executor, or ReactivePromise into a ComputedPromise β
an object that is both awaitable like a regular Promise and reactive like a
Computed signal.
Awaitable:await promise(fetch(...)) resolves to the fulfilled value,
or rejects with the rejection reason, just like a native Promise.
Reactive: calling the returned value as a function (cp()) reads the
current result inside an effect or computed, tracking it as a dependency.
Equivalent to .result β returns undefined while pending, the fulfilled
value when resolved, or the rejection reason when rejected.
Reactive state is also accessible via:
.state β "pending" | "fulfilled" | "rejected"
.value β the resolved value (or undefined while pending)
.reason β the rejection reason (or undefined while pending/fulfilled)
.result β T | E | undefined; the resolved value, rejection reason, or undefined while pending
Wraps a promise, executor, or ReactivePromise into a ComputedPromise β
an object that is both awaitable like a regular Promise and reactive like a
Computed signal.
Awaitable:await promise(fetch(...)) resolves to the fulfilled value,
or rejects with the rejection reason, just like a native Promise.
Reactive: calling the returned value as a function (cp()) reads the
current result inside an effect or computed, tracking it as a dependency.
Equivalent to .result β returns undefined while pending, the fulfilled
value when resolved, or the rejection reason when rejected.
Reactive state is also accessible via:
.state β "pending" | "fulfilled" | "rejected"
.value β the resolved value (or undefined while pending)
.reason β the rejection reason (or undefined while pending/fulfilled)
.result β T | E | undefined; the resolved value, rejection reason, or undefined while pending
Wraps a promise, executor, or ReactivePromise into a ComputedPromise β
an object that is both awaitable like a regular Promise and reactive like a
Computed signal.
Awaitable:await promise(fetch(...)) resolves to the fulfilled value,
or rejects with the rejection reason, just like a native Promise.
Reactive: calling the returned value as a function (cp()) reads the
current result inside an effect or computed, tracking it as a dependency.
Equivalent to .result β returns undefined while pending, the fulfilled
value when resolved, or the rejection reason when rejected.
Reactive state is also accessible via:
.state β "pending" | "fulfilled" | "rejected"
.value β the resolved value (or undefined while pending)
.reason β the rejection reason (or undefined while pending/fulfilled)
.result β T | E | undefined; the resolved value, rejection reason, or undefined while pending
A ComputedPromise is a real Promise β you can await it, chain .then() / .catch() / .finally(), and use it anywhere a Promise is expected:
constdata=await cp;
cp.then(value=> console.log(value))
.catch(err=> console.error(err));
Reactive
Calling the ComputedPromise as a function inside an effect or computed tracks it as a reactive dependency:
Pending β returns undefined
Fulfilled β returns the resolved value
Rejected β throws the rejection reason
import { effect } from"elements-kit/signals";
effect(() => {
try {
constvalue=cp(); // undefined while pending, T when fulfilled
console.log("value:", value);
} catch (err) {
console.error("error:", err);
}
});
State Properties
All three state properties are reactive β reading them inside an effect or computed subscribes to changes:
cp.state; // "pending" | "fulfilled" | "rejected"
cp.value; // T | undefined
cp.reason; // E | undefined
Result
.result is a convenience property that returns T | E | undefined β the resolved value when fulfilled, the rejection reason when rejected, or undefined while pending. Useful when you donβt need to distinguish between fulfilled and rejected but just want the current value:
effect(() => {
console.log("current result:", cp.result);
});
ReactivePromise
ReactivePromise is the lower-level class that powers ComputedPromise. Use it directly when you need to construct or wrap a promise and expose its reactive state without the Computed callable interface:
import {
classReactivePromise<T, E=unknown>
A Promise subclass that exposes its state as reactive signals.
Prefer the
promise
factory for most use cases β it returns a
ComputedPromise that is both awaitable and callable as a signal.
Use ReactivePromise directly when you need the lower-level class β
for example, to wrap a promise and expose .state, .value, .reason,
and .result without the Computed callable interface.
A Promise subclass that exposes its state as reactive signals.
Prefer the
promise
factory for most use cases β it returns a
ComputedPromise that is both awaitable and callable as a signal.
Use ReactivePromise directly when you need the lower-level class β
for example, to wrap a promise and expose .state, .value, .reason,
and .result without the Computed callable interface.
Attaches a callback for only the rejection of the Promise.
@param β onrejected The callback to execute when the Promise is rejected.
@returns β A Promise for the completion of the callback.
catch(
reject: (reason?:any) =>void
reject);
});
// Wrap an existing promise
const
constrp2:ReactivePromise<Response, unknown>
rp2=
classReactivePromise<T, E=unknown>
A Promise subclass that exposes its state as reactive signals.
Prefer the
promise
factory for most use cases β it returns a
ComputedPromise that is both awaitable and callable as a signal.
Use ReactivePromise directly when you need the lower-level class β
for example, to wrap a promise and expose .state, .value, .reason,
and .result without the Computed callable interface.