# java-promise
**Repository Path**: mirrors_andyglick/java-promise
## Basic Information
- **Project Name**: java-promise
- **Description**: Promise library for java.You can do the same thing with JavaScript's Promise in java! Concurrent library.
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-01-02
- **Last Updated**: 2026-02-14
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Overview
**java-promise** is a Promise Library for Java.
- You can easily control asynchronous operations like JavaScript's **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**.
- Supports both synchronous and asynchronous execution.
It is licensed under [MIT](https://opensource.org/licenses/MIT).
[](https://maven-badges.herokuapp.com/maven-central/org.riversun/java-promise)
# Quick Look
**Writing a Promise in Javascript**
A typical example of using promise in JavaScript is:
```JavaScript
Promise.resolve('foo')
.then(function (data) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
const newData = data + 'bar';
resolve(newData);
}, 1);
});
})
.then(function (data) {
return new Promise(function (resolve, reject) {
console.log(data);
});
});
console.log("Promise in JavaScript");
```
Result:
```
Promise in JavaScript
foobar
```
**Writing a Promise in java-promise**
Write the same thing using **java-promise**
```Java
import org.riversun.promise.Promise;
public class Example {
public static void main(String[] args) {
Promise.resolve("foo")
.then(new Promise((action, data) -> {
new Thread(() -> {
String newData = data + "bar";
action.resolve(newData);
}).start();
}))
.then(new Promise((action, data) -> {
System.out.println(data);
action.resolve();
}))
.start();
System.out.println("Promise in Java");
}
}
```
Result:
```
Promise in Java
foobar
```
**Syntax:**
Yes,you can write in a syntax similar to JavaScript as follows:
```Java
Promise.resolve()
.then(new Promise(funcFulfilled1), new Promise(funcRejected1))
.then(new Promise(functionFulfilled2), new Promise(functionRejected2))
.start();
```
# Dependency
**Maven**
```xml
org.riversunjava-promise1.1.0
```
**Gradle**
```
compile group: 'org.riversun', name: 'java-promise', version: '1.1.0'
```
# Quick Start
### Execute sequentially by chained "then"
- Use **Promise.then()** to chain operations.
- Write your logic in **Func.run(action,data)**.
- Start operation by **Promise.start** and run **asynchronously**(run on worker thread)
- Calling **action.resolve** makes the promise **fullfilled** state and passes the **result** to the next then
```java
public class Example00 {
public static void main(String[] args) {
Func function1 = (action, data) -> {
new Thread(() -> {
System.out.println("Process-1");
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
//Specify result value.(Any type can be specified)
action.resolve("Result-1");
}).start();
};
Func function2 = (action, data) -> {
System.out.println("Process-2 result=" + data);
action.resolve();
};
Promise.resolve()
.then(new Promise(function1))
.then(new Promise(function2))
.start();// start Promise operation
System.out.println("Hello,Promise");
}
}
```
**Diagram:**
**Result:**
```
Hello,Promise
Process-1
Process-1 result=Result-1
```
**Tips**
It's also OK to just write ``Promise.then(func) ``.
```Java
Promise.resolve()
.then(function1)
.then(function2)
.start();// start Promise operation
```
# Description
### What is "**Func**" ?
**Func** is a java interface equivalent to JavaScript's **Function** for argument of **#then**
```java
public interface Func {
public void run(Action action, Object data) throws Exception;
}
```
You can write **Func** like a JavaScript function.
I want to show two ways of implementing **Func** class.
No.1)Write **Func** object in the normal way.
```Java
Func function = new Func() {
@Override
public void run(Action action, Object data) throws Exception {
System.out.println("Process");//write your logic
action.resolve();
}
};
```
No.2)Write **Func** object using lambda expression.
```Java
Func function = (action, data) -> {
System.out.println("Process");//write your logic
action.resolve();
};
```
### What is "**Action**" ?
Action object is an argument of **Func#run** method.
- Call **action.resolve( [fulfillment value] )** to make the Promise's status **fulfilled** and move on to the next processing(specified by then) with the result(**fulfillment value**).
```Java
action.resolve("Success");
```
- Call **action.reject( [rejection reason] )** to make the Promise's status **rejected** and move on to the next processing(specified by then) with the result(**rejection reason**).
```Java
action.reject("Failure");
```
- Argument is optional, you can call **action.resolve()** or **action.reject()**
```Java
action.resolve();//Argument can be omitted
```
# Usage
### Rejection
If **action.reject()** is called, or if an **exception thrown** while executing **Func.run()**, **rejected** status is set to Promise, and the **onRejected** function specified to **then** is called.
- call ``action.reject``
```Java
Func function = (action, data) -> {
action.reject("Failure");
};
```
- throw an exception
```Java
Func function = (action, data) -> {
throw new Exception("something");
};
```
Let's see **Promise.then()** method,
the **2nd argument** of **Promise.then()** can be set to a **Func** to receive the result of **rejection** when receiving the result of **then**.
- **Syntax**
Usage ``Promise.then(onFulfilled[, onRejected]);``
- **onFulfilled** is a **Func** object called if the Promise is fulfilled.
You can receive the previous execution **"fulfilled"** result as an argument named **data**.
- **onRejected** is a **Func** object called if the Promise is rejected.
You can receive the previous execution **"rejected"** result(mainly the objects are exceptions) as an argument named **data**.
```Java
//Rejection
public class ExampleRejection {
public static void main(String[] args) {
Promise.resolve()
.then((action, data) -> {
System.out.println("Process-1");
action.reject();
})
.then(
// call when resolved
(action, data) -> {
System.out.println("Resolved Process-2");
action.resolve();
},
// call when rejected
(action, data) -> {
System.out.println("Rejected Process-2");
action.resolve();
})
.start();// start Promise operation
System.out.println("Hello,Promise");
}
}
```
**Diagram:**
**Result:**
```
Hello,Promise
Process-1
Rejected Process-2
```
### Promise.always
**Promise.always()** always receive both **fulfilled** and **rejected** results.
```Java
public class ExampleAlways {
public static void main(String[] args) {
Func func2OutReject = (action, data) -> {
action.reject("I send REJECT");
//action.resolve("I send RESOLVE");
};
Func func2ReceiveAlways = (action, data) -> {
System.out.println("Received:" + data);
action.resolve();
};
Promise.resolve()
.then(func2OutReject)
.always(func2ReceiveAlways)
.start();
}
}
```
**Diagram:**
**Result**
```
Received:I send REJECT
```
### Promise.all
Execute multiple promises at the same time, and after all executions are complete, move to the next processing with then
- Execute multiple promises simultaneously and wait until all the execution is finished before proceeding.
- If all finishes with resolve, execution results will be stored as **java.util.List