@JsType(isNative=true,
namespace="<global>",
name="Promise")
public class Promise<T>
extends java.lang.Object
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
A Promise is in one of these states:
pending: initial state, neither fulfilled nor rejected.fulfilled: meaning that the operation was completed successfully.rejected: meaning that the operation failed.A pending promise can either be fulfilled with a value or rejected with a reason (error). When either of these options happens, the associated handlers queued up by a promise's then method are called. If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.
| Constructor and Description |
|---|
Promise(PromiseExecutor<T> executor)
Creates a new Promise object.
|
| Modifier and Type | Method and Description |
|---|---|
static <V> Promise<JsArray<V>> |
all(JsIterable<Promise<? extends V>> promises)
The Promise.all() method takes an iterable of promises as an input, and returns a single Promise
that resolves to an array of the results of the input promises.
|
static <V> Promise<JsArray<V>> |
all(Promise<? extends V>... promises)
The Promise.all() method takes an iterable of promises as an input, and returns a single Promise
that resolves to an array of the results of the input promises.
|
static <V> Promise<V> |
any(JsIterable<Promise<? extends V>> promises)
Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the
iterable fulfills, returns a single promise that resolves with the value from that promise.
|
static <V> Promise<V> |
any(Promise<? extends V>... promises)
Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the
iterable fulfills, returns a single promise that resolves with the value from that promise.
|
<V> Promise<V> |
catch_(OnRejectedCallback<? extends V> onRejected)
Appends a rejection handler callback to the promise, and returns a new promise resolving to the
return value of the callback if it is called, or to its original fulfillment value if the promise
is instead fulfilled.
|
Promise<T> |
finally_(OnSettledCallback onSettle)
The finally() method returns a Promise.
|
static <V> Promise<V> |
race(JsIterable<Promise<? extends V>> promises)
The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.
|
static <V> Promise<V> |
race(Promise<? extends V>... promises)
The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.
|
static <V> Promise<V> |
reject(java.lang.Object reason)
Returns a new Promise object that is rejected with the given reason.
|
static <V> Promise<V> |
resolve(Promise<V> value)
Returns a new Promise object that is resolved with the given value.
|
static <V> Promise<V> |
resolve(V value)
Returns a new Promise object that is resolved with the given value.
|
<V> Promise<V> |
then(OnFulfilledCallback<? super T,? extends V> onFulfilled)
Appends the fulfillment handler to the promise, and returns a new promise resolving to the
return value of the called handler.
|
<V> Promise<V> |
then(OnFulfilledCallback<? super T,? extends V> onFulfilled,
OnRejectedCallback<? extends V> onRejected)
Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the
return value of the called handler, or to its original settled value if the promise was not handled
(i.e.
|
Promise<java.lang.Void> |
thenAccept(AcceptCallback<? super T> onAccept)
Appends the fulfillment handler to the promise, and returns a new promise resolving to a void value.
|
public Promise(@Nonnull
PromiseExecutor<T> executor)
executor - a function executed by the constructor during the process of constructing the promise. The executor is custom code that ties an outcome to a promise.@SafeVarargs @Nonnull @JsOverlay public static <V> Promise<JsArray<V>> all(@Nonnull Promise<? extends V>... promises)
V - the component type of the promise returned.promises - the promises.@Nonnull public static <V> Promise<JsArray<V>> all(@Nonnull JsIterable<Promise<? extends V>> promises)
V - the component type of the promise results.promises - the promises.@SafeVarargs @Nonnull @JsOverlay public static <V> Promise<V> any(@Nonnull Promise<? extends V>... promises)
V - the component type of the promise returned.promises - the promises.AggregateError containing the reasons for all the rejected promises.@Nonnull public static <V> Promise<V> any(@Nonnull JsIterable<Promise<? extends V>> promises)
V - the component type of the promise returned.promises - the promises.AggregateError containing the reasons for all the rejected promises.@SafeVarargs @Nonnull @JsOverlay public static <V> Promise<V> race(@Nonnull Promise<? extends V>... promises)
V - the component type of the promise returned.promises - the promises.@Nonnull public static <V> Promise<V> race(@Nonnull JsIterable<Promise<? extends V>> promises)
V - the component type of the promise returned.promises - the promises.@Nonnull public static <V> Promise<V> resolve(@Nullable Promise<V> value)
V - the component type of the promise returned.value - the promise to wrap and return.@Nonnull public static <V> Promise<V> resolve(@Nullable V value)
Generally, if you don't know if a value is a promise or not, Promise.resolve(value) it instead and work with the return value as a promise.
V - the component type of the promise returned.value - the value to use when creating the promise.@Nonnull public static <V> Promise<V> reject(@Nullable java.lang.Object reason)
V - the component type of the promise returned.reason - the reason to reject promise with.@Nonnull public <V> Promise<V> then(@Nonnull OnFulfilledCallback<? super T,? extends V> onFulfilled)
V - the component type of the promise returned from the callback.onFulfilled - the callback function called when the promise is fulfilled.@JsMethod(name="then") @Nonnull public Promise<java.lang.Void> thenAccept(@Nonnull AcceptCallback<? super T> onAccept)
onAccept - the callback function called when the promise is fulfilled.@JsMethod(name="catch") @Nonnull public <V> Promise<V> catch_(@Nonnull OnRejectedCallback<? extends V> onRejected)
V - the component type of the promise returned from the callback.onRejected - the callback function called when the promise is rejected.@Nonnull public <V> Promise<V> then(@Nullable OnFulfilledCallback<? super T,? extends V> onFulfilled, @Nullable OnRejectedCallback<? extends V> onRejected)
V - the component type of the promise returned from the callback.onFulfilled - the callback function called when the promise is fulfilled.onRejected - the callback function called when the promise is rejected.@JsMethod(name="finally") @Nonnull public Promise<T> finally_(OnSettledCallback onSettle)
onSettle - a callback function called when the Promise is settled.