Skip to content

Latest commit

 

History

History
173 lines (148 loc) · 4.6 KB

README.md

File metadata and controls

173 lines (148 loc) · 4.6 KB

GDeferred

GDeferred is a fork of JDeferred - "Java Deferred/Promise library similar to JQuery's Deferred Object" - reimplemented for GWT.

Features

  • Deferred Object and Promise
  • Promise callbacks
    • .then(…)
    • .done(…)
    • .fail(…)
    • .progress(…)
    • .always(…)
  • Multiple promises (soon!)
    • .when(p1, p2, p3, …).then(…)
  • Java Generics support
    • Deferred<Integer, Exception, Double> deferred;
    • deferred.resolve(10);
    • deferred.reject(new Exception());
    • deferred.progress(0.80);
  • Java 8 Lambda friendly

Quick Examples

Deferred Object and Promise

Deferred deferred = new DeferredObject();
Promise promise = deferred.promise();
promise.done(new DoneCallback() {
  public void onDone(Object result) {
    ...
  }
}).fail(new FailCallback() {
  public void onFail(Object rejection) {
    ...
  }
}).progress(new ProgressCallback() {
  public void onProgress(Object progress) {
    ...
  }
}).always(new AlwaysCallback() {
  public void onAlways(State state, Object result, Object rejection) {
    ...
  }
});

With the reference to deferred object, you can then trigger actions/updates:

deferred.resolve("done");
deferred.reject("oops");
deferred.progress("100%");

Filter

Filtering allows one to return a different promise of the same state.

Deferred d = …;
Promise p = d.promise();
Promise filtered = p.then(new DoneFilter<Integer, Integer>() {
  public Integer filterDone(Integer result)
    return result * 10;
  }
});

filtered.done(new DoneCallback<Integer>{
  public void onDone(Integer result) {
    // result would be original * 10
    System.out.println(result);
  }
});

d.resolve(3) -> 30.

Pipe

Piping allows one to return a different promise of the any state.

Deferred d = ...;
Promise p = d.promise();

p.then(new DonePipe<Integer, Integer, Exception, Void>() {
  public Deferred<Integer, Exception, Void> pipeDone(Integer result) {
    if (result < 100) {
      return new DeferredObject<Integer, Void, Void>().resolve(result);
    } else {
      return new DeferredObject<Integer, Void, Void>().reject(new Exception(...));
    }
  }
}).done(...).fail(...);

d.resolve(80) -> done!
d.resolve(100) -> fail!

Java 8 Lambda

Now this is pretty cool when used with Java 8 Lambda!

dm.when(() -> {
  return "Hey!";
}).done(r -> System.out.println(r));

dm.when(
  () -> { return "Hello"; },
  () -> { return "World"; }
).done(rs ->
  rs.forEach(r -> System.out.println(r.getResult()))
);

Documentation

Downloads

GDeferred is currently available at maven central.

Maven

<dependency>
    <groupId>io.reinert.gdeferred</groupId>
    <artifactId>gdeferred</artifactId>
    <version>0.9.0</version>
</dependency>

License

GDeferred is freely distributable under the Apache 2.0 License

<script type="text/javascript"> /* */ </script> <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"> </script>