https://github.com/ioweyou/iou-java
IOU for Java - Promises/A+ implementation for Java
https://github.com/ioweyou/iou-java
aplus iou java promise
Last synced: 6 months ago
JSON representation
IOU for Java - Promises/A+ implementation for Java
- Host: GitHub
- URL: https://github.com/ioweyou/iou-java
- Owner: ioweyou
- License: mit
- Archived: true
- Created: 2016-04-19T18:05:53.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-03-02T07:40:58.000Z (over 9 years ago)
- Last Synced: 2025-07-23T13:51:09.704Z (12 months ago)
- Topics: aplus, iou, java, promise
- Language: Java
- Homepage: https://ioweyou.github.io
- Size: 13.7 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# IOU Java
[](https://travis-ci.org/ioweyou/iou-java)
[](http://badges.mit-license.org)
IOU Java is a [Promises/A+](https://github.com/promises-aplus/promises-spec) compliant promise library that extends [IOU Core](https://github.com/ioweyou/iou-core).
## Maven
-----
```xml
nl.brusque.iou
iou-java
1.0.0-beta-01
```
## Gradle
-----
```
compile 'nl.brusque.iou:iou-java:1.0.0-beta-01' { transitive = true }
```
## Example
-----
### Call with single then
```java
IOU iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable() {
@Override
public Void apply(Integer input) throws Exception {
System.out.println(input.toString());
return null;
}
});
iou.resolve(42); // prints "42"
```
### Chained or piped promise
```java
IOU iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable() {
@Override
public Integer apply(Integer input) throws Exception {
return input * 10;
}
})
.then(new IThenCallable() {
@Override
public String apply(Integer input) throws Exception {
return String.format("The result: %d", input);
}
})
.then(new IThenCallable() {
@Override
public Void apply(String input) throws Exception {
System.out.println(input);
return null;
}
});
iou.resolve(42); // prints "The result: 420"
```
### Rejecting a promise
```java
IOU iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable() {
@Override
public Integer apply(Integer integer) throws Exception {
return integer * 42;
}
})
.fail(new IThenCallable() {
@Override
public Void apply(Object input) throws Exception {
System.out.println(String.format("%s I can't do that.", input));
return null;
}
});
iou.reject("I'm sorry, Dave."); // prints "I'm sorry, Dave. I can't do that."
```
Or if you like the A+ way better
```java
IOU iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable() {
@Override
public Integer apply(Integer input) throws Exception {
return input * 42;
}
}, new IThenCallable() {
@Override
public Integer apply(Object input) throws Exception {
System.out.println(String.format("%s I can't do that.", input));
return null;
}
});
iou.reject("I'm sorry, A+."); // prints "I'm sorry, A+. I can't do that."
```
### Failing a promise
```java
IOU iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable() {
@Override
public Integer apply(Integer input) throws Exception {
throw new Exception("I just don't care.");
}
})
.then(new IThenCallable() {
@Override
public Void apply(Integer input) throws Exception {
System.out.println("What would you say you do here?");
return null;
}
})
.fail(new IThenCallable() {
@Override
public Void apply(Object reason) throws Exception {
System.out.println(
String.format("It's not that I'm lazy, it's that %s",
((Exception)reason).getMessage()));
return null;
}
});
iou.resolve(42); // prints "It's not that I'm lazy, it's that I just don't care."
```