Package arez.promise
Class ObservablePromise<T,E>
java.lang.Object
arez.promise.ObservablePromise<T,E>
- Type Parameters:
T
- the type of the value that the promise will resolve to.E
- the type of the error if the promise is rejected.
An observable model that wraps a Promise and exposes observable state that track
the state of the promise. The observable exposes the state of the promise as well
as the value that it resolves to or the error it was rejected with as observable
properties.
A very simple example
import akasha.Console;
import akasha.Response;
import akasha.WindowGlobal;
import akasha.promise.Promise;
import arez.Arez;
import arez.promise.ObservablePromise;
import com.google.gwt.core.client.EntryPoint;
public class Example
implements EntryPoint
{
public void onModuleLoad()
{
final Promise<Response> fetch = WindowGlobal.fetch( "https://example.com/" );
final ObservablePromise<Response, Object> observablePromise = ObservablePromise.create( promise );
Arez.context().observer( () -> Console.log( "Promise Status: " + observablePromise.getState() ) );
}
}
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
E> ObservablePromise<T, E> create
(akasha.promise.Promise<T> promise) Create the observable model that wraps specified promise.getError()
Return the error that the promise was rejected with.getState()
Return the promise state.getValue()
Return the value that the promise was resolved to.
-
Method Details
-
create
@Nonnull public static <T,E> ObservablePromise<T,E> create(@Nonnull akasha.promise.Promise<T> promise) Create the observable model that wraps specified promise.- Type Parameters:
T
- the type of the value that the promise will resolve to.E
- the type of the error if the promise is rejected.- Parameters:
promise
- the promise to wrap.- Returns:
- the ObservablePromise
-
getState
Return the promise state.- Returns:
- the promise state.
-
getValue
Return the value that the promise was resolved to. This should NOT be called if the state is notObservablePromise.State.FULFILLED
and will result in an invariant failure if invariants are enabled.- Returns:
- the value that the promise was resolved to.
-
getError
Return the error that the promise was rejected with. This should NOT be called if the state is notObservablePromise.State.REJECTED
and will result in an invariant failure if invariants are enabled.- Returns:
- the error that the promise was rejected with.
-