Git Product home page Git Product logo

android-promiser's Introduction

Android Promiser

A lightweight implementation of Promises/A+ specification.

Gradle

Add the following dependency to build.gradle:

dependencies {
    ...
    compile 'com.octopepper.promiser:android-promiser:1.0.2'
}

Requirements

This library is written using Java 8 syntax.

Let's embrace the future ! ๐Ÿ˜„

To use this into Java 7, 6 and 5 projects, don't forget to install retrolambda by @orfjackal.

Usage

You can create a Promiser object like this :

Promiser<T, U> p = new Promiser<T, U>(
    (Resolver<T> resolve, Rejecter<U> reject) -> {

        // Place your asynchronous process here, and make sure
        // to trigger resolve.run() or reject.run() when needed.

});

<T> is the type of the result returned in case of success and <U> is the type of the result returned in case of error.

You can handle result and error cases like this now :

p.success((T result) -> {
      // Handle success here

    }).error((U err) -> {
      // Handle error here

    });

If you want to do multiple asynchronous tasks sequentially use .then()

p.then((T result) -> {
      // T instanceOf String
      // example X = parseToInt(T)

    }).then((X result) -> {
      // Now X instanceOf int

    });

Example

For example let's mock an asynchronous process using Retrofit v2

public Promiser<String, Integer> fetchUsers() {
        return new Promiser<>((resolve, reject) -> {
            retrofit.getService(IUserService.class).fetchUsers().enqueue(new Callback<String>() {
                @Override
                public void onResponse(Response<String> response) {
                    if (response.isSuccess())
                        resolve.run(response.body()); //resolving result
                    else
                        reject.run(response.code()); //rejecting error code
                }

                @Override
                public void onFailure(Throwable t) {
                    reject.run(CodeError.Undefined.getCode()); //reject error
                }
            });
        });
}

Now we can handle the success or the error of this promise using the .success() and .error() callbacks :

fetchUsers()
    .success(str -> {
      // Handle success here

    }).error(code -> {
      // Handle error here

    });
fetchUsers()
    .then(str -> {
      // Parse Json to Object

    }).then(users -> {
      // Do something

    });

or even better:

fetchUsers()
    .success(this::resultSucceeded)
    .error(this::resultError);

private void resultSucceeded(String s) {
  // Handle success here

}

private void resultError(Integer code) {
  // Handle error here

}

Next step

  • Make .then() accept a Promiser as a return type. The Promiser returned will have to be resolved.
fetchUser()
    .then(user -> fetchPhotosByUser())
    .then(photos -> {
      // Do something

    });

Contributors

NodensN, YannickDot, s4cha

android-promiser's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.