Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Promise.then method for handling cases where the value is Void … #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static CompletableFuture<Void> allOf(CompletableFuture<?>... futures) {
}

CompletableFuture<Void> future = new CompletableFuture<>();
and(futures).then((value, reason) -> {
and(futures).then((reason) -> {
if (reason != null) {
future.tryCompleteThrowable(reason);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package java.util.concurrent.impl;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
*
Expand Down Expand Up @@ -64,4 +65,11 @@ public void then(Runnable callback) {
JsPromise.OnSettledCallback func = value -> callback.run();
jsPromise.then(func, func);
}

public void then(final Consumer<? super Throwable> callback) {
assert callback != null;
jsPromise.then(
value -> callback.accept(null),
reason -> callback.accept((Throwable) reason));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package java.util.concurrent.impl;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
*
Expand All @@ -29,4 +30,6 @@ public interface Promise<V> {
void then(BiConsumer<? super V, ? super Throwable> callback);

void then(Runnable callback);

void then(Consumer<? super Throwable> callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
*
Expand Down Expand Up @@ -58,6 +59,12 @@ public void then(Runnable callback) {
}
}

@Override
public void then(Consumer<? super Throwable> callback) {
assert callback != null;
then(() -> callback.accept(reason));
}

private void complete(V value, Throwable reason) {
if (!done) {
this.value = value;
Expand Down