Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/reinert/gdeferred

GWT Deferred/Promise API similar to jQuery.
https://github.com/reinert/gdeferred

Last synced: 20 days ago
JSON representation

GWT Deferred/Promise API similar to jQuery.

Awesome Lists containing this project

README

        

GDeferred
=========
GDeferred is a fork of [JDeferred](https://github.com/jdeferred/jdeferred.git) - "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!](https://github.com/reinert/gdeferred/issues/1))
* ```.when(p1, p2, p3, …).then(…)```
* Java Generics support
* ```Deferred deferred;```
* ```deferred.resolve(10);```
* ```deferred.reject(new Exception());```
* ```deferred.progress(0.80);```
* Java 8 Lambda friendly

Quick Examples
==============

Deferred Object and Promise
---------------------------

```java
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:

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

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

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

filtered.done(new DoneCallback{
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.

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

p.then(new DonePipe() {
public Deferred pipeDone(Integer result) {
if (result < 100) {
return new DeferredObject().resolve(result);
} else {
return new DeferredObject().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!

```Java
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
* [Javadocs](http://reinert.github.io/gdeferred/javadoc/apidocs/index.html)

## Downloads
GDeferred is currently available at maven central.

### Maven
```

io.reinert.gdeferred
gdeferred
0.9.0

```

## License
GDeferred is freely distributable under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0.html)

/* <![CDATA[ */
var google_conversion_id = 974052972;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "wsVZCOycvgkQ7Ly70AM";
var google_conversion_value = 0;
var google_remarketing_only = false;
/* ]]> */