T
- the type of values instances of this class resolve to in case they complete normally.public interface Promise<T>
Promises allow waiting for the outcome of an operation in an async manner. Instead of waiting synchronously, callbacks, so called continuations, can be registered that are invoked on completion.
Async functions returning promises can be composed to build async flows.
Continuations are run on the AsyncScheduler
a promise was created with. Methods of this
class that return a Promise
, like continueOnUi(Func1)
, catchOnUi(Func1)
,
etc., capture the AsyncScheduler
as returned by AsyncSchedulers.current()
at the
time the method is called and associate it with the returned Promise
.
Most often promises are produced and continuations registered on the UI thread, which implies,
that continuations will also run on the UI thread and subsequent Promise
s are also
associated with the UI thread. This is why functions for registering continuations contain
the term 'Ui'. Running all functionality on the UI thread makes synchronization between async
functions very easy, because there is no multi-threading involved. This implies, however, that
these functions may only run for a very short period of time in order not to compromise the user
experience. Long running operations should be run on a separate worker thread by switching to an
according AsyncScheduler
or by using Async.executeAsync(Func)
.
The following exemplary code demonstrates an asynchronous method that returns a Promise.
public Promise<String> getStringAsync() { PromiseSource<String> promiseSource = new PromiseSource<>(); // Trigger a long-running operation getStringFromServer((text, ex) -> { if (ex) { promiseSource.setException(ex); return; } promiseSource.setResult(text); }); return promiseSource.getPromise(); }
The example code below illustrates how to chain promises:
getTextFromServer() .continueAsyncOnUi(text -> uploadTextToAnotherServer(text)) .continueOnUi(result -> { Log.d(TAG, "Upload completed: " + result); return null; }) .catchOnUi(e -> { Log.e(TAG, "Something went wrong.", e); return null; }) .conclude();
Modifier and Type | Method and Description |
---|---|
<TConvert> Promise<TConvert> |
asConst(TConvert value)
Converts this
Promise into one returning the specified value. |
Promise<Void> |
asVoid()
|
Promise<T> |
cancelable(CancellationToken cancellationToken,
boolean forceImmediately)
Allows a promise chain being cancelled.
|
Promise<T> |
catchAsyncOnUi(Func1<AsyncException,Promise<T>,RuntimeException> exceptionHandler)
Registers an async function which is invoked on this instance's scheduler if this task completes
exceptionally.
|
Promise<T> |
catchOnUi(Func1<AsyncException,T,? extends Exception> exceptionHandler)
Registers an
Action which is invoked on this instance's scheduler if this task completes
exceptionally. |
void |
conclude()
Concludes an async flow.
|
<TNext> Promise<TNext> |
continueAsyncOnUi(Func1<? super T,Promise<TNext>,? extends Exception> continuation)
Registers an async function which will be invoked on this instance's scheduler as soon as the underlying
task completed successfully.
|
<TNext> Promise<TNext> |
continueAsyncOnUiWithAsyncResult(Func1<AsyncResult<? extends T>,Promise<TNext>,? extends Exception> continuation)
Registers an async function which will be invoked on this instance's scheduler as soon as the underlying
task completed successfully.
|
<TNext> Promise<TNext> |
continueOnUi(Func1<? super T,TNext,? extends Exception> continuation)
Registers a function which will be invoked on this instance's scheduler as soon as the underlying task
has completed successfully.
|
<TNext> Promise<TNext> |
continueOnUiWithAsyncResult(Func1<AsyncResult<? extends T>,TNext,? extends Exception> continuation)
Registers a function which will be invoked on this instance's scheduler as soon as the underlying task is
completed successfully.
|
Promise<T> |
finallyAsyncOnUi(Func<Promise<Void>,? extends Exception> finallyBlock)
Registers an async function which will be invoked on this instance's scheduler as soon as the underlying
task is completed, regardless of whether it completes successfully or exceptionally.
|
Promise<T> |
finallyOnUi(Action<? extends Exception> finallyBlock)
Registers an
Action which is invoked on this instance's scheduler as soon as the underlying
task is completed, regardless of whether it completes successfully or exceptionally. |
AsyncResult<? extends T> |
getResult()
Returns the promised async result if this promise is completed.
|
void |
register(AsyncCallback<? super T> continuation)
Registers a callback which will be invoked on this instance's scheduler with an
AsyncResult ,
as soon as the underlying task is completed. |
AsyncResult<? extends T> getResult()
void register(AsyncCallback<? super T> continuation)
AsyncResult
,
as soon as the underlying task is completed.
The passed on async result's AsyncResult.get()
will raise an AsyncException
if the underlying task failed.continuation
- the callback that will be invoked on this instance's scheduler as soon as the
underlying task is completed.<TNext> Promise<TNext> continueOnUi(Func1<? super T,TNext,? extends Exception> continuation)
Promise
will be associated with the scheduler current at the time this
method is invoked.continuation
- the callback that will be invoked on this instance's scheduler as soon as the
underlying task is completed successfully.Promise
reflecting the outcome of this instance if exceptional, or the
outcome of the continuation otherwise.<TNext> Promise<TNext> continueOnUiWithAsyncResult(Func1<AsyncResult<? extends T>,TNext,? extends Exception> continuation)
Promise
will be associated with the scheduler current at the time this
method is invoked.continuation
- the callback that will be invoked on this instance's scheduler as soon as the
underlying task is completed.Promise
reflecting the outcome of this instance if exceptional, or the
outcome of the continuation otherwise.<TNext> Promise<TNext> continueAsyncOnUi(Func1<? super T,Promise<TNext>,? extends Exception> continuation)
Promise
will be associated with the scheduler current at the time this
method is invoked.continuation
- the callback that will be invoked on this instance's scheduler as soon as the
underlying task is completed.Promise
reflecting the outcome of this instance if exceptional, or the
outcome of the continuation otherwise.<TNext> Promise<TNext> continueAsyncOnUiWithAsyncResult(Func1<AsyncResult<? extends T>,Promise<TNext>,? extends Exception> continuation)
Promise
will be associated with the scheduler current at the time this
method is invoked.continuation
- the callback that will be invoked on this instance's scheduler as soon as the
underlying task is completed.Promise
reflecting the outcome of this instance if exceptional, or the
outcome of the continuation otherwise.Promise<T> finallyOnUi(Action<? extends Exception> finallyBlock)
Action
which is invoked on this instance's scheduler as soon as the underlying
task is completed, regardless of whether it completes successfully or exceptionally.
The returned Promise
will be associated with the scheduler current at the time this
method is invoked.finallyBlock
- the action that will be invoked on this instance's scheduler as soon as the
underlying task is completed.Promise<T> finallyAsyncOnUi(Func<Promise<Void>,? extends Exception> finallyBlock)
Promise
will be associated with the scheduler current at the time this
method is invoked.finallyBlock
- the async function that will be invoked on this instance's scheduler as soon as the
underlying task is completed.Promise<T> catchOnUi(Func1<AsyncException,T,? extends Exception> exceptionHandler)
Action
which is invoked on this instance's scheduler if this task completes
exceptionally. In this case the returned Promise
will reflect the outcome of the
registered exceptionHandler. Otherwise it will reflect the result of this instance.
The returned Promise
will be associated with the scheduler current at the time this
method is invoked.exceptionHandler
- the function that will be invoked on this instance's scheduler if the underlying
Promise
completes exceptionally.Promise<T> catchAsyncOnUi(Func1<AsyncException,Promise<T>,RuntimeException> exceptionHandler)
Promise
will reflect the outcome of the
registered async exceptionHandler. Otherwise it will reflect the result of this instance.
The returned Promise
will be associated with the scheduler current at the time this
method is invoked.exceptionHandler
- the async function that will be invoked on this instance's scheduler if the
underlying Promise
completes exceptionally.Promise<Void> asVoid()
Promise
into one returning null of type Void
. If this instance
completes normally, the returned promise will complete with null, otherwise it will reflect
the Exception
this instance resolves to.
The returned Promise
will be associated with the scheduler current at the time this
method is invoked.Void
.<TConvert> Promise<TConvert> asConst(TConvert value)
Promise
into one returning the specified value. If this instance
completes normally, the returned promise will complete with the given value, otherwise it
will reflect the Exception
this instance resolves to.
The returned Promise
will be associated with the scheduler current at the time this
method is invoked.void conclude()
Promise
s which aren't
resolved otherwise. This is to make sure, the final outcome of an async flow isn't lost. This
is especially useful if an async flow ends exceptionally. In this case, if the last
promise returned by the chain of async calls isn't handled, the Exception
it
resolves to would get lost. A call to this function ensures, any exception that hasn't
been handled is treated as unhandled exception.Promise<T> cancelable(CancellationToken cancellationToken, boolean forceImmediately)
Promise
to
a CancellationException
.
Note: If the operation represented by this Promise is to be interrupted, the
CancellationToken must be passed to the corresponding function directly, which must handle
cancellation.
The returned Promise
will be associated with the scheduler current at the time this
method is invoked.cancellationToken
- the cancellation token.forceImmediately
- when set to false, the current Promise will run to an end, before
the returned Promise is completed. If set to true, the returned
Promise is completed immediately if the cancellationToken requests
cancellation.