Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/yannickdot/promiser-java

A lightweight implementation of promises in Java
https://github.com/yannickdot/promiser-java

Last synced: about 6 hours ago
JSON representation

A lightweight implementation of promises in Java

Awesome Lists containing this project

README

        

# Promiser-java

A lightweight implementation of [Promises/A+](https://promisesaplus.com) specification, inspiration taken from JavaScript.

## 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](https://github.com/orfjackal/retrolambda) by [@orfjackal](https://github.com/orfjackal).

### Usage

You can create a Promiser object like this :
``` java
Promiser p = new Promiser((Resolver resolve, Rejecter reject) -> {

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

});
```

`` is the type of the result returned in case of success and `` is the type of the result returned in case of error.

You can handle result and error cases like this now :
```java
p.success((T result) -> {
// Handle success here

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

})
```
---

### Example

For example let's mock an asynchronous process using a Timer :
``` java
Promiser p = new Promiser((Resolver resolve, Rejecter reject) -> {
int DELAY = 500;

new Timer().schedule(new TimerTask() {
@Override
public void run() {
resolve.run("I'm done !"); //resolving
reject.run(404); //rejecting
}
}, DELAY);

});
```

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

``` java
p.success((String result) -> {
// Handle success here
})
.error((Integer err) -> {
// Handle failure here
})
```

## Next step

* Make a Promiser instance "thenable" so we can have a `.then()` and `.catch()` callbacks and provide an asynchronous flow control using `.then()` like this :

```java
p.then(...)
.then(...)
.then(...)
.catch(...)

```