Skip to content

Commit

Permalink
resolveIt -> resolveValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ibb-jile committed Sep 21, 2020
1 parent c7c6d55 commit 2c1f9b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ public class Promise<T> {
private PromiseState state;
private Promise child;

public static <T> Promise<T> resolveIt(T value) {
public static <T> Promise<T> resolveValue(T value) {
return new Promise<>(promise -> {
promise.resolve(value);
});
}

public static <T> Promise<T> resolveIt(StartCallback<T> callback) {
public static <T> Promise<T> resolveValue(StartCallback<T> callback) {
return new Promise<>(callback);
}

public static <T> Promise<T> resolveIt(StartCallbackWithoutPromise<T> callback) {
return resolveIt(promise -> promise.resolve(callback.run()));
public static <T> Promise<T> resolveValue(StartCallbackWithoutPromise<T> callback) {
return resolveValue(promise -> promise.resolve(callback.run()));
}

public static Promise<Void> all(Promise[] promises) {
Expand Down
24 changes: 11 additions & 13 deletions AndroidPromise/src/test/java/PromiseTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import org.ibbjile.androidPromise.ExceptionCallback;
import org.ibbjile.androidPromise.Promise;
import org.ibbjile.androidPromise.StartCallback;
import org.ibbjile.androidPromise.ThenCallback;
import org.ibbjile.androidPromise.ThenCallbackWithoutPromise;
import org.ibbjile.androidPromise.VoidCallback;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -20,7 +18,7 @@ public class PromiseTest {
public void justResolve() throws InterruptedException, ExecutionException, TimeoutException {
final CompletableFuture<String> future = new CompletableFuture<>();

Promise.resolveIt("Hello")
Promise.resolveValue("Hello")
.done(value -> {
future.complete(value);
});
Expand All @@ -32,7 +30,7 @@ public void justResolve() throws InterruptedException, ExecutionException, Timeo
public void catchInResolveIt() throws InterruptedException, ExecutionException, TimeoutException {
final CompletableFuture<Exception> future = new CompletableFuture<>();

Promise.resolveIt((p) -> p.resolve(this.simulateException()))
Promise.resolveValue((p) -> p.resolve(this.simulateException()))
.done(value -> {
future.cancel(true);
})
Expand All @@ -42,7 +40,7 @@ public void catchInResolveIt() throws InterruptedException, ExecutionException,

final CompletableFuture<Exception> future1 = new CompletableFuture<>();

Promise.resolveIt(() -> this.simulateException())
Promise.resolveValue(() -> this.simulateException())
.done(value -> {
future1.cancel(true);
})
Expand All @@ -59,7 +57,7 @@ private String simulateException() throws Exception {
public void chainingPromise() throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<String> future = new CompletableFuture<>();

Promise<String> promise = Promise.resolveIt("hello")
Promise<String> promise = Promise.resolveValue("hello")
.then((String result) -> result + "_world")
.then((String result) -> result + "_people")
.then((String result) -> result + ":" + result.length());
Expand All @@ -72,7 +70,7 @@ public void chainingPromise() throws InterruptedException, ExecutionException, T

Promise<String> promise2 = new Promise<>((result) -> result + "_people");

promise = Promise.resolveIt("hello")
promise = Promise.resolveValue("hello")
.then((result, p) -> p.resolve(result + "_world"))
.then(promise2)
.then((String result) -> result.length())
Expand All @@ -87,7 +85,7 @@ public void chainingPromise() throws InterruptedException, ExecutionException, T
public void changeTypeInChain() throws InterruptedException, ExecutionException, TimeoutException {
final CompletableFuture<Integer> future = new CompletableFuture<>();

Promise.resolveIt("hello")
Promise.resolveValue("hello")
.then((String result) -> result + ":" + result.length())
.then((String result) -> result.length())
.done((Integer result) -> future.complete(result));
Expand All @@ -102,7 +100,7 @@ public void asyncInChain() throws InterruptedException, ExecutionException, Time
final Promise[] delayedPromise = new Promise[1];


Promise.resolveIt("hello")
Promise.resolveValue("hello")
.done((result, p) -> delayedPromise[0] = p)
.then((String result) -> result.length())
.done(future::complete);
Expand All @@ -118,7 +116,7 @@ public void errorInAsyncChain() throws InterruptedException, ExecutionException,

final Promise[] delayedPromise = new Promise[1];

Promise.resolveIt("hello")
Promise.resolveValue("hello")
.done((result, p) -> delayedPromise[0] = p)
.then((String result) -> result.length())
.fail(e -> future.complete((Exception) e));
Expand All @@ -132,7 +130,7 @@ public void errorInAsyncChain() throws InterruptedException, ExecutionException,
public void catchErrorWithReject() throws InterruptedException, ExecutionException, TimeoutException {
final CompletableFuture<Exception> future = new CompletableFuture<>();

Promise.resolveIt("hello")
Promise.resolveValue("hello")
.then((String result) -> result + ":" + result.length())
.then((String result) -> result + "aaaaaaaaaaaa")
.done((result, p) -> p.reject(new Exception("ouu")))
Expand All @@ -147,7 +145,7 @@ public void catchErrorWithReject() throws InterruptedException, ExecutionExcepti
public void catchErrorWithThrow() throws InterruptedException, ExecutionException, TimeoutException {
final CompletableFuture<Exception> future = new CompletableFuture<>();

Promise.resolveIt("hello")
Promise.resolveValue("hello")
.then((String result) -> result + ":" + result.length())
.then((String result) -> result + "aaaaaaaaaaaa")
.then((String result) -> {
Expand All @@ -171,7 +169,7 @@ public void joinNewPromiseToChain() throws InterruptedException, ExecutionExcept
p.resolve(s + "-promise2");
});

Promise.resolveIt("hello")
Promise.resolveValue("hello")
.done((result, p) -> delayedPromise[0] = p)
.then((String result) -> result.length())
.then(promise2)
Expand Down

0 comments on commit 2c1f9b6

Please sign in to comment.