public interface Promise<A> extends Future<A>
Future. It can be claimed without needing to
catch checked exceptions, and it may be mapped to new types of Promise via
the map(Function) and flatMap(Function) methods.
For instance, if you have a Promise<A> and you want to do
some operation on the value (an A) you can use map(Function) to turn
this into a Promise of some other type. Let's say you get back a
Person and you really only need their surname:
public Promise<String> fetchSurname(PersonId id) {
Promise<Person> promise asyncClient.fetchPerson(id);
return promise.map(new Function<Person, String>() {
public String apply(Person p) {
return p.surname();
}
};
}
If you want to do some further asynchronous operation using the value, you
can use flatMap(Function) to turn this into a Promise of some other
type. Let's say you get back a Person and you really only need
to perform a further query to get their address:
public Promise<Address> fetchAddress(PersonId id) {
Promise<Person> promise asyncClient.fetchPerson(id);
return promise.flatMap(new Function<Person, Promise<Address>>() {
public Promise<Address> apply(Person p) {
return asyncClient.fetchAddress(p.addressId());
}
};
}
Note that there are a number of handy utility functions for creating
Promise objects on the
Promises companion.
Cancelling a Promise that hasn't yet been completed will do it with a
CancellationException and that will propagate to
dependent Promises. But cancelling a dependent Promise will not cancel the
original one.
| Modifier and Type | Interface and Description |
|---|---|
static interface |
Promise.TryConsumer<A>
Consumer interface to be called after a promise is fulfilled with a
succesful value or a failure.
|
| Modifier and Type | Method and Description |
|---|---|
A |
claim()
Blocks the thread waiting for a result.
|
Promise<A> |
done(Consumer<? super A> c)
Registers a callback to be called when the promised object is available.
|
Promise<A> |
fail(Consumer<Throwable> c)
Registers a callback to be called when an exception is thrown.
|
<B> Promise<B> |
flatMap(Function<? super A,? extends Promise<? extends B>> function)
Transforms this promise from one type to another by way of a transformation
function that returns a new Promise, leaving the strategy for that promise
production up to the function.
|
<B> Promise<B> |
fold(Function<Throwable,? extends B> handleThrowable,
Function<? super A,? extends B> function)
Transform this promise from one type to another, also providing a strategy
for dealing with any exceptions encountered.
|
<B> Promise<B> |
map(Function<? super A,? extends B> function)
Transforms this
Promise from one type
to another by way of a transformation function. |
Promise<A> |
recover(Function<Throwable,? extends A> handleThrowable)
Recover from an exception using the supplied exception strategy
|
Promise<A> |
then(Promise.TryConsumer<? super A> callback)
Registers a TryConsumer to handle both success and failure (exception)
cases.
|
A claim()
Promise<A> done(Consumer<? super A> c)
c - The consumer to call with the resultPromise<A> fail(Consumer<Throwable> c)
c - The consumer to call with the throwablePromise<A> then(Promise.TryConsumer<? super A> callback)
callback - The future callback<B> Promise<B> map(Function<? super A,? extends B> function)
Promise from one type
to another by way of a transformation function.
B - a B.function - The transformation function<B> Promise<B> flatMap(Function<? super A,? extends Promise<? extends B>> function)
Note this is known as flatMap as it first maps to a
Promise<Promise<A>> and then flattens that out
into a single layer Promise.
B - a B.function - The transformation function to a new Promise valuePromise<A> recover(Function<Throwable,? extends A> handleThrowable)
handleThrowable - rehabilitate the exception with a value of type B<B> Promise<B> fold(Function<Throwable,? extends B> handleThrowable, Function<? super A,? extends B> function)
B - a B.handleThrowable - rehabilitate the exception with a value of type Bfunction - mapping functionCopyright © 2016 Atlassian. All rights reserved.