Skip to Content
API reference

PromiseStages

Adapters between promises and Java CompletionStage.

AsyncType: typescriptnative.PromiseStagesArtifact: com.margelo:typescript-native-stdlibJVM only

PromiseStages class

public final class PromiseStages

CompletionStage adapters for Java 8 and Android API 24 or core library desugaring.

Static methods

static <T> CompletableFuture<T> toStage(Promise<T> promise)

Observes a promise through a new future. Fulfillment completes the future with the original value. Rejection completes it exceptionally with Promise.exception. An Error is wrapped in a CompletionException whose fixed message reads no error property. This also prevents later dependent stages from formatting an error after its message changes. Other Throwables pass unchanged; other reasons, including null, are held in a Promise.Rejection. A first-stage failure handler sees that wrapper; its cause is the original Error. Explicit JDK formatting, including Future.get(), can read a message getter. Propagating JDK dependents can format a raw native Throwable; the adapter's private cleanup does not.

Observation uses Promise.observe. On an execution owner, completion runs as a later job of that owner. Without one, it runs on the settling thread, or immediately on the calling thread for an already settled promise. Failed owner registration falls back to unowned observation, so completion can also run inline while the owner remains live. A failure from a still-live host is reported before that fallback. No executor is added for future dependents; their own CompletionStage scheduling rules apply.

The subscription is disposed when the returned future completes for any reason, including cancellation or manual completion. Cancellation suppresses an observation not yet taken for execution; it cannot interrupt a callback already taken, cancel the promise's producer, or affect other consumers. A stopped observation owner can prevent completion from the promise.

T
the fulfillment type
promise
the non-null promise to observe
Returns
a new future with a disposable observation of the promise
Throws NullPointerException
if promise is null
static <T> Promise<T> fromStage(CompletionStage<? extends T> stage)

Resolves a new promise when a stage completes. A successful value is passed to Promise.resolve, which follows it if it is itself a promise. On failure, one CompletionException with a non-null cause is unwrapped, then Promise.reason unwraps one Promise.Rejection carrier. Other failures are kept unchanged. Thus an ordinary value, Throwable, or non-Throwable rejection carried by toStage retains its identity on return, except for these explicit wrapper and promise-value conversions.

The adapter uses CompletionStage.whenComplete, with no executor of its own. Settlement runs on the thread chosen by the stage for that callback, possibly before this method returns for an already completed stage. Construction and fulfillment need no event loop; consumers choose their own reaction owners. Rejection before an accepted observation needs a reporting owner, captured from a live owner at construction or at rejection. Create the adapter on a supported owner if the stage can fail before an observation is registered. Inside EventLoop.run, this producer keeps the run alive until its completion callback settles the promise. A null stage or a registration that throws retains no keep-alive. Other hosts keep their own lifetime rules.

Stage cancellation is treated as a failure. The registered stage callback is not detachable through this API. Settling the returned promise independently, or disposing one of its observations, does not cancel the stage or remove that callback. In a round trip through toStage, cancelling the returned future only disposes its promise observation; it does not cancel the original stage.

T
the fulfillment type
stage
the non-null source stage
Returns
a new promise whose outcome follows completion of the stage unless independently claimed first
Throws NullPointerException
if stage is null

Generated when the website builds, from PromiseStages.java.

Language

TypeScriptNo TypeScript typeJVMJava and KotlinSwiftNot generated yet

Remembered on every API page

On this page