View on GitHub


Download this project as a .zip file Download this project as a tar.gz file


A deferred object allows you to separate the following two concerns:

Using the api should be easy and straight forward without too much boiler plate. To execute code in the background:

Promise<String> promise = Function.execute(new Callable<String>() {
    public String call() throws Exception {
        // This method will run on a background thread
        return "Hello world!";

Or in Android studio it will be readable like this:

Promise<String> promise = execute(() -> {return "Hello world!";});

This promise can be used to add tasks that needs to be executed on success or on error. An example:

promise.thenOnMainThread(new Task<String>() {
    public void run(String message) {
        Toast.makeText(getContext(), message, LENGTH_LONG);
.onErrorOnMainThread(new Task<Throwable>() {
    public void run(Throwable data) {
        Toast.makeText(getContext(), "Oops!", LENGTH_LONG);

And in Android Studio it will look like:

promise.thenOnMainThread(() -> {Toast.makeText(getContext(), message, LENGTH_LONG);})
       .onErrorOnMainThread(() -> {Toast.makeText(getContext(), "Oops!", LENGTH_LONG);});

You don't have to worry about the promise already been resolved or not. If the promise is not resolved, the task will be scheduled to be executed when the promise is resolved. The task will be executed immediately if the promise is already resolved. The order of the tasks is respected for tasks that are scheduled on the same thread.


You can schedule tasks to be executed on different threads: