Skip to content
Asynchronous promise implementation for Android.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Q-android is a Asynchronous Promise implementation for Android.

This library brings the concept of JS Promises to Android and it's similar to the DeferredObject in jQuery and the Android Deferred Object.

A Defered is a chainable utility object which can register multiple callbacks into callbacks queues, invoque callbacks queues, and relay the success or failure statete of any aysnc or async function.

Defered allow to run pieces of code preventing other code from interfering with the progress or status of its internal request. You can define callback chains using the then and error methods and have access to actual resolve and reject (and Defered.all) methods which is useful to resolve Promises.


Instantiate a Deferred. The Deferred defines the type of return value:

Q.Promise promise = new Q.Promise();

Then you can define callbacks for your Deferred:

promise.then( new Q.Promise.Callback<Boolean, Void>(){
         public Void result(Boolean result) {
             return null;
.error(new Q.Promise.ErrorCallback() {
    public void error(Exception e) {

Then you can resolve or reject your Deferred in any other part of the code.

// e.g. after an async operation
if (error != null) {
    defered.reject("error msg");

In the code above if an "error" occurs and the promise is rejected then the error() method in the Deferred gets call. If no error and the resolve method is called then the deferred will be resolved passing the value "true" as the result param in the result callback, so the assert will be true.


The Promise exposes only the Deferred methods needed to attach additional handlers or determine the state: then, error and the ones to change the state: resolve, reject:

Main API methods:

  • then( callback/promise )
  • error( callback/promise )
  • resolve( value )
  • reject( value )

Static methods:

  • Defered.defer():
  • Defered.all()

Callbacks objects:

  • Deferred.ICallback: This is the main Interface.
  • Deferred.Callback: Callback for passing a deferred promise result.
  • Deferred.AsyncCallback: Callback for passing a deferred promise result with an asynchronous chained result. The result method returns a Deferred.
  • Deferred.ErrorCallback: Callback for passing a deferred promise error.


Resolve the promise by passing a deferred result or deferred result.


Reject the promise by passing an error.


Add a promise result callback. It's useful to run define secuential process with async calls. e.g. Promise1 -> Promise2 -> Promise3. See example:

    public Deferred<Boolean> promise1() {
        return Deferred.defer( true );
    public Deferred<Boolean> promise2() {
        return Deferred.defer( false );
    public Deferred<Boolean> promise3() { 
        return Deferred.defer( null );
        .then((Deferred.ICallback<Boolean, Object>) promise2())
        .then((Deferred.ICallback<Object, Object>) promise3());    

The promise2 won't be resolved until promise1 has finished. promise3 won't be resolved until promise2 has finished.


Add a promise reject callback.


This brings a lot of options on operations on defereds, like for example build a lists of promises and use the method to resolve them, see an example:

    List<Deferred<Boolean>> deferreds = new ArrayList<Deferred<Boolean>>();
    Deferred.all( deferreds )
            .then(new Deferred.AsyncCallback<List<Boolean>, Object>() {
                public Deferred<Object> result(List<Boolean> result) {
                    assertEquals(expectedresult, result);
                    return null;

You can find more examples about operations in the Q test cases


Promises became useful when defining function which return other promises. They help to:

  • controler process flow with async calls.
  • write cleaner calls to async methods.
  • manage error messages over a number of operations.
  • multiplatform: Port for iOS.

Use cases

We will include to some links in code to show more examples.


Documentation includes the following:

  • high level overview of usage
  • detailed description of api usage
  • api docs
  • and maybe something on use cases
You can’t perform that action at this time.